Operations 8 min read

Master Linux Text Viewing: cat, tac, more, less, head, tail, sort, sed, uniq, vi

This guide introduces essential Linux commands for viewing and manipulating text files—including cat, tac, more, less, head, tail, sort, sed, uniq, and vi—explaining their basic usage, common options, and practical examples for effective command‑line text handling.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Text Viewing: cat, tac, more, less, head, tail, sort, sed, uniq, vi

Full Text Display – cat

The cat command outputs the entire content of a file to the terminal. Common forms:

cat file               # display full text
cat -n file            # display text with line numbers

It can also concatenate files:

cat file1 file2 >file3  # merge file1 and file2 into file3

Reverse Display – tac

tac

works like cat but prints lines in reverse order.

tac file

Paginated Display – more

more

shows a file page by page, useful for large files.

Display content: more file Navigate with keys: Enter (down one line), Space (down one screen), b (up one screen), = (show current line number), :f (show file name and line), q (quit).

Start from a specific line: more +10 file (starts at line 10).

Start from a matching string: more +/string file (starts two lines before the matching line).

Interactive Browsing – less

less

provides forward and backward navigation, plus richer search capabilities.

less file               # view file
less -N file           # view with line numbers
less -m file           # view with percentage progress

Key bindings:

f   # forward one screen
b   # backward one screen
Enter or j   # forward one line
k   # backward one line
G   # go to last line
g   # go to first line
/ string   # search forward
? string   # search backward
q   # quit

Multiple files can be opened and switched with :n (next), :p (previous), :x (first), and :d (remove from list).

Show File Header – head

head

displays the beginning of a file.

head -n 100 file   # first 100 lines
head -n -100 file  # all but the last 100 lines

Show File Footer – tail

tail

displays the end of a file and can follow updates.

tail -100 file      # last 100 lines
tail -n +100 file   # from line 100 onward
tail -f logFile     # continuously display new lines appended to logFile

Sorted Display – sort

sort

orders lines in a file. Examples:

Alphabetical ascending: sort test.txt Descending: sort -r test.txt Remove duplicates: sort -u test.txt Numeric sort:

sort -n file

Filter Display – sed

sed

is a stream editor; here we show simple text‑viewing uses.

Print lines containing a keyword: sed -n "/string/p" logFile Print specific line ranges:

sed -n "1,5p" logFile          # lines 1‑5
sed -n '3,5{=;p}' logFile      # lines 3‑5 with line numbers
sed -n "10p" logFile           # line 10

Remove Duplicate Lines – uniq

Common uniq usages:

uniq file               # delete duplicate lines
uniq -c file            # show duplicate count
uniq -d file            # show only duplicated lines
uniq -u file            # show only unique lines
uniq -i file            # ignore case when removing duplicates
uniq -w 10 file         # treat first 10 characters as the key

Text Editing – vi

Open a file for viewing/editing with vi file. The more powerful vim offers many additional features (not covered here).

Conclusion

Linux provides a rich set of commands for viewing and processing text. Choose the appropriate tool based on the task—simple display with cat, paginated browsing with more or less, selective extraction with head, tail, sed, uniq, or sorting with sort. For advanced usage, consult the manual pages or combine commands (e.g., ps -elf | more).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxCATcommand-linelesssorttext-viewing
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.