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.
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 numbersIt can also concatenate files:
cat file1 file2 >file3 # merge file1 and file2 into file3Reverse Display – tac
tacworks like cat but prints lines in reverse order.
tac filePaginated Display – more
moreshows 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
lessprovides 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 progressKey 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 # quitMultiple files can be opened and switched with :n (next), :p (previous), :x (first), and :d (remove from list).
Show File Header – head
headdisplays the beginning of a file.
head -n 100 file # first 100 lines
head -n -100 file # all but the last 100 linesShow File Footer – tail
taildisplays 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 logFileSorted Display – sort
sortorders 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 fileFilter Display – sed
sedis 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 10Remove 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 keyText 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).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
