Skip to main content
In Linux, nearly every interaction—SSH sessions, command outputs, system logs, and configuration files—is plain text. Mastering text filters allows you to view, transform, and compare these streams efficiently at the command line.

Viewing Files with cat, tac, head, and tail

Displaying Entire and Reversed Files

Use cat for quick, on-screen dumps of small files:
To flip the order (last line first), pipe through tac:

Inspecting the Start or End of Large Logs

Log files can grow huge. Quickly grab the first or last N lines:
  • Last 10 lines (default):
    tail /var/log/dnf.log
  • Last 20 lines:
    tail -n 20 /var/log/dnf.log
  • First 20 lines:
    head -n 20 /var/log/dnf.log
These let you preview recent errors or initial startup messages without opening the full file.

Automating In-File Replacements with sed

The stream editor sed excels at find-and-replace tasks:
  1. Preview changes (no file modified):
  2. Apply in-place (-i) substitutions:
  • s/pattern/replacement/g replaces all occurrences on each line.
  • The -i flag edits the file directly.
Always preview your sed commands without -i first. To keep a backup, use -i.bak (e.g., sed -i.bak 's/old/new/g' file).

Extracting Fields with cut

When working with delimited data (spaces, commas, or tabs), cut slices out columns:
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.
  • By space delimiter: extract the first field (name)
  • By comma delimiter: extract the third field (country) and save

Listing Unique Entries with sort and uniq

The uniq filter only removes adjacent duplicates—sort first to catch all duplicates:
If your file isn’t sorted, uniq may leave non-adjacent duplicates. Always sort before uniq for a full cleanse.

Comparing Files with diff

Spot differences between configuration files using:
  • Basic side-by-side:
  • Unified context (-c):
  • Two-column view (-y):
This helps pinpoint changes before editing or deploying configurations.

Quick Reference: Linux Text Filters

Watch Video