Skip to main content
In this article, we explore essential Linux command-line tools for comparing and manipulating file content. Because Linux relies heavily on text—whether for SSH sessions, configuration files, or log files—mastering these commands will improve your efficiency in managing and troubleshooting your system.

Viewing File Content

To inspect a small file quickly, use the cat command with the filename. For example:
If you prefer to see the contents in reverse order (from bottom to top), the tac command is available:
For longer files, such as logs, it is often more practical to view only a portion of the file. The tail command displays the last 10 lines by default, which is useful for checking the most recent log entries. Conversely, the head command shows the beginning of a file. For instance, consider a log file with 10 lines (keep in mind that empty lines might also count):
You can also control the number of lines displayed by using the -n option with both tail and head.

Automating Text Replacement with SED

Editing multiple instances manually in large files can be error-prone and time-consuming. The Stream Editor (SED) automates search and replace tasks efficiently. For example, if a file listing user details has the country “Canada” misspelled as “canda”, you can preview the correction with:
Let’s break down the command:
  • s/canda/canada/g: The substitute command where canda is replaced with canada globally on each line.
  • Single quotes ensure Bash does not interpret special characters.
  • The -g flag replaces all occurrences in each line.
Once you’re satisfied with the preview, apply the change in-place:
Always back up your files before performing in-place edits with sed -i.
It is important to quote the expression correctly to prevent Bash from misinterpreting special characters such as the asterisk. Both single and double quotes can be used:

Extracting Data with Cut

The cut command is ideal for extracting specific columns from a file. For example, to extract the first column—which often contains names—from a space-separated file, use:
Here, -d ' ' sets the delimiter to a space, while -f 1 specifies that the first field should be extracted. If the file is comma-separated, simply adjust the delimiter. For instance, to extract the third field (which could represent country names) and save the output to countries.txt, run:
In this command, the redirection operator (>) saves the extracted output to a new file.

Removing Duplicate Entries

After extracting data—like a list of countries—you might encounter duplicate entries. The uniq command removes duplicates from adjacent lines. For example:
To remove duplicates effectively, sort the file first so that similar lines are adjacent, then pipe the output to uniq:
Piping (|) is a powerful technique that allows you to pass the output from one command directly into another for further processing.

Comparing Files with Diff

When system upgrades or configuration changes modify files, comparing the old and new versions is crucial. The diff command highlights these differences. Consider the following example:
In this output, the notation 1c1 indicates that line 1 of file1 differs from line 1 of file2. The < symbol shows content from file1, while > represents content from file2. For more context, use the -c option:
For a side-by-side visual comparison, use the -y option:
Alternatively, you can use sdiff for a similar side-by-side comparison:
Using the diff command with different options (-c, -y, or sdiff) can help you pinpoint changes more easily during system upgrades or when troubleshooting configuration issues.

Summary

This guide introduced a variety of Linux commands—cat, tac, head, sed, cut, sort, uniq, and diff—that are invaluable for viewing, editing, and comparing file content. Mastery of these tools not only streamlines your workflow but also enhances your ability to manage and debug files in any Linux environment. For more detailed explanations and advanced use cases, consider exploring additional Linux command-line resources.

Watch Video