Beginner Linux commands for text files
A practical command-line reference for finding your way around, inspecting text, and doing common cleanup tasks without losing track of the original data.
Start with folders and files
Linux commands run in a current working directory. These commands help you see where you are and move around:
pwd # print the current directory ls # list visible files ls -la # include hidden files and details cd ~/TinyDevTools # change to the project directory
Use mkdir -p scratch/text to create a folder and any missing parent folders. To keep an original file untouched while experimenting, copy it first: cp -- input.txt scratch/text/input.txt. The -- marks the end of command options, which helps when a filename begins with a dash.
Inspect a text file
For a small file, cat -- notes.txt prints the whole file. For a long file, less -- application.log lets you browse it; press q to quit. To check only the beginning or end, use:
head -n 10 -- application.log tail -n 20 -- application.log wc -l -- application.log
wc -l counts newline characters, so a final line without a newline may not be included in the count. That distinction can matter when comparing line counts across files.
Search logs and text
grep prints lines matching a pattern. Add -n to show line numbers, and quote the search text so the shell passes it as one argument:
grep -n -- 'ERROR' application.log grep -n -- 'connection timed out' application.log
Use find to locate files by name. This searches below the current directory for text files:
find . -type f -name '*.txt'
Count and sort lines
Unix text tools can be combined with a pipe (|): the output of one command becomes the input of the next. For example, this counts repeated lines after sorting them together:
sort -- names.txt | uniq -c
uniq only combines identical adjacent lines, which is why this example sorts first. Sorting changes the original order. For a sorted list with duplicates removed, use sort -u -- names.txt. Keep the original if order matters, or use Remove Duplicate Lines to preserve the first-seen order in the browser. See Understanding sort and uniq for the details.
Save results without overwriting input
The > operator writes command output to a file, replacing that file if it already exists. Choose a different output filename so the source remains available:
sort -u -- names.txt > names-sorted.txt
Do not redirect output to the same file being read by a command; the shell can truncate the file before the command reads it. Avoid rm until you are certain which path it will remove, especially when using wildcards.
Get help for a command
On most Linux systems, man grep opens the manual page for grep; press q to leave it. Many commands also accept --help, for example sort --help. Manuals describe options that may differ between Linux distributions and other Unix-like systems.
These commands run in your local shell and are useful for quick file workflows. TinyDevTools provides related browser-based operations for finding duplicates, sorting lines, and trimming whitespace.