Skip to main content
Linux is built around text. Whether you’re working in an SSH session, configuring applications, or modifying system settings, you’ll encounter numerous text files. In this article, we explore common commands used to view, edit, transform, and compare file content, providing practical examples to improve your workflow.

Viewing Files

For small files, the cat command allows you to display the entire file content quickly. For example, if you have a file named “users.txt”, you can run:
If you wish to display the file contents in reverse order (i.e., with the last line first), use the tac command, which works similarly to cat but in reverse. For large files such as system logs, you might be interested only in the most recent entries. The tail command displays the last 10 lines by default. You can specify a different number of lines with the -n option:
To display the beginning of a file, the head command shows the first 10 lines by default. Similar to tail, you can modify the output by using the -n option:
For files with warnings or error messages, consider piping the output to a pager like less to navigate the content easily.

Automating Text Manipulation with sed

Manually editing large files can be tedious, especially when you need to update many occurrences of the same text. The sed (stream editor) utility automates text transformations. Imagine you have a file named “userinfo.txt” that includes addresses, phone numbers, and citizenship details. If the country “Canada” is misspelled as “canda” throughout the file, you can preview the correction with:
In this command:
  • The s stands for substitute.
  • s/canda/canada/g tells sed to replace every occurrence of “canda” with “canada” on each line (the g flag indicates a global replacement).
After confirming the output, apply the changes in-place using the -i option:
Always preview changes before applying them with the -i option to avoid accidental modifications.

Extracting Specific File Content

Sometimes you need only a part of a file. For instance, to extract the first column (e.g., names) from “userinfo.txt” when fields are space-delimited, use the cut command:
Here:
  • The -d option defines the delimiter (a space in this case).
  • The -f option selects the field (column 1).
If your file uses commas as delimiters and you want to extract the third field (perhaps representing countries), you can direct the output to a new file:

Removing Duplicate Lines

If the output file (e.g., “countries.txt”) contains duplicate entries, the uniq command removes adjacent duplicate lines. However, since it only works with consecutive duplicates, sort the file first:
Then, remove duplicates with:

Comparing Files

When configuration files change during an upgrade, comparing the old and new versions is crucial. The diff command isolates differences between files, making it easier to identify what has changed. For a simple comparison:
Example output:
This indicates that line 1 in file1 differs from line 1 in file2 (denoted by 1c1), and similarly for line 4. Here, < shows content from the first file and > shows content from the second file. For additional context, use the context option (-c):
For those who prefer a side-by-side comparison, use the -y option or the sdiff command:
In these side-by-side outputs, a pipe (|) separates the differing content.

Extracting Names Using Cut

If you need to extract just the names (the first column) from “userinfo.txt”, the command is:
The image shows a terminal interface with a command prompt on the left and a text file named "userinfo.txt" on the right, containing a list of names, cities, countries, and numbers.

Removing Duplicates and Sorting

After extracting data, such as a list of countries, you might notice duplicates. Combining the sort and uniq commands will both sort the entries and remove duplicates, ensuring an ordered list.
The image shows a terminal interface with a file named "countries.txt" containing a list of country names, including duplicates. The title "uniq and sort" suggests a task involving sorting and removing duplicates.

Conclusion

In this article, we’ve covered essential Linux utilities for managing text files, including:
  • Viewing files with cat, tac, tail, and head
  • Modifying content with sed
  • Extracting columns with cut
  • Removing duplicates and sorting using sort and uniq
  • Comparing files with diff and sdiff
These tools are indispensable for system administration and everyday Linux operations. With practice, you’ll be better equipped to efficiently manage and analyze text files. Happy scripting!

Watch Video