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.

Commands on this page were checked on Linux with GNU coreutils 9.4 and the installed awk. Options and output formatting can differ on macOS and other Unix-like systems, so consult the local manual page before reusing a command there. The examples use small plain-text files; they do not parse CSV or JSON records.

Start with folders and files

Linux commands run in a current working directory. These commands help you see where you are and move around. Replace the example path with a directory that exists on your machine:

pwd                         # print the current directory
ls                          # list visible files
ls -la                      # include hidden files and details
cd /path/to/your/project    # change to your 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. This isolated example creates a temporary working directory and counts a short list after sorting it:

workdir=$(mktemp -d) || exit
cd "$workdir" || exit
printf '%s\n' beta alpha beta > names.txt
LC_ALL=C sort -- names.txt | uniq -c

Output (counts shown without alignment spaces):
1 alpha
2 beta

uniq only combines identical adjacent lines, which is why this example sorts first. The LC_ALL=C setting makes the ASCII sample's ordering predictable; locale affects sorting, especially for non-ASCII or mixed-case text. Sorting changes the original order. For a sorted list with duplicates removed, use LC_ALL=C 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 more detail and the GNU uniq manual.

Save results without overwriting input

The > operator writes command output to a file, replacing that file if it already exists. In the temporary directory from the previous example, this writes the sorted unique lines to a new file and leaves names.txt unchanged:

LC_ALL=C sort -u -- names.txt > names-sorted.txt
cat -- names-sorted.txt

Output:
alpha
beta

Choose a new output filename or verify that overwriting an existing destination is intended. 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.

Advertisements