Master Linux Text Viewing: Essential Commands and Practical Tips
This guide introduces the most useful Linux text‑viewing commands—including cat, tac, more, less, head, tail, sort, sed, uniq, and vi—explaining their basic syntax, common options, and practical examples for efficient file inspection and manipulation.
Full‑text display with cat
The cat command outputs an entire file to the terminal. Basic usage:
cat file # display full content
cat -n file # display with line numbersIt can also concatenate files:
cat file1 file2 >file3 # merge file1 and file2 into file3Reverse display with tac
tacis cat reversed; it prints a file line‑by‑line in reverse order.
tac filePaginated view with more
moreshows a file page by page, useful for large files. more file Common navigation keys:
Enter – scroll down one line (default)
Space – scroll down one screen
b – scroll up one screen
= – display current line number
:f – show file name and line number
q – quit
Start from a specific line: more +10 file # start display at line 10 Start from a matching string:
more +/string file # start two lines before the line containing "string"Interactive browsing with less
lessprovides forward and backward navigation, plus powerful search.
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 the last line
g – go to the first line
/string – search forward for "string"
?string – search backward for "string"
q – quit
Switch between multiple files:
less file1 file2 file3
:n # next file
:p # previous file
:x # first file
:d # remove current file from the listShow file header with 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 tail with 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 as the file growsSorted display with sort
sortorders lines in a file. Examples:
# Ascending (default)
sort test.txt
# Descending
sort -r test.txt
# Remove duplicate lines
sort -u test.txt
# Numeric sort
sort -n fileUse -r for reverse order, -u to drop duplicates, and -n for numeric comparison.
Filtering with sed
sedis a stream editor; here we show only viewing‑related features.
# 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 onlyRemove duplicate lines with uniq
uniqfilters adjacent duplicate lines.
uniq file # drop duplicate lines
uniq -c file # show duplicate count
uniq -d file # only duplicated lines
uniq -u file # only unique lines
uniq -i file # ignore case
uniq -w 10 file # compare first 10 charactersQuick file view with vi
Open a file in the classic editor: vi file For more powerful editing, consider vim, which extends vi with many features (not covered here).
Conclusion
Linux offers a rich set of text‑viewing utilities. Choose the command that matches your workflow—simple display with cat, paginated browsing with more or less, selective extraction with head / tail, sorting with sort, filtering with sed, deduplication with uniq, or quick editing with vi. For deeper options, consult the manual pages.
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.
