Master Linux Log Viewing: Essential Commands for Efficient File Inspection
This guide teaches backend engineers and Linux users how to safely inspect file contents—including large log files—using core commands such as cat, tac, more, less, head, tail, and grep, while providing practical examples, option explanations, and common pitfalls to avoid.
Introduction
For backend developers, quickly examining log files on Linux is a routine task, but using the wrong tool can overload memory or crash a server. This article explains how to view files safely, especially large ones, and introduces a small shell script to generate test data.
# Generate 10 lines of test data (modify as needed)
for ((i=1;i<=10;i++)); do
echo "第${i}行" >> test.txt
if [[ `expr $i % 2` -eq 0 ]]; then
echo -e >> test.txt
fi
doneDirect File Viewing with cat
The cat command (and its variants tac and nl) can display an entire file. Useful options include:
-A : combine -vET to show non‑printable characters.
-b : number non‑blank lines.
-E : display end‑of‑line $.
-n : number all lines, including blanks.
-T : show tabs as ^I.
-v : reveal otherwise invisible characters.
Examples:
cat test.txtcat -n test.txt(show line numbers)
For very large files, avoid cat because it loads the whole file into memory.
Reverse Viewing with tac
tacprints a file in reverse order (last line first). Example:
tac test.txtPaging with more and less
moreprovides simple paging; less offers full navigation, forward and backward search, and more commands.
more
Key more controls:
Space: next page
Enter: next line
less
Key less controls:
PageDown / PageUp: page navigation
/string: forward search
?string: backward search
n / N: repeat previous search
g / G: go to first / last line
q: quit
Data Extraction with head and tail
headprints the first n lines (default 10). Use -n to specify the count; a negative number skips the last x lines.
Example:
head -n 10 test.txttailprints the last lines. The -f option follows the file, useful for live logs.
Options:
-n +X: start from line X.
-f : continuously display new data.
Examples:
tail -n 5 test.txttail -f test.txt(live view)
Common Utilities
Combine commands with pipes ( |) to process output.
Example:
ll | head -n 3grepsearches for patterns within files. Example:
cat -n test.txt | grep '999'Redirection >> appends output to a file, e.g., echo "第$i行" >> test.txt.
Use wc to count bytes, lines, or words. Example: wc -l test.txt shows the number of lines.
Practical Case
Print lines 11‑20 of a log file:
head -n 20 text.txt | tail -n 10With line numbers:
cat -n test.txt | head -n 20 | tail -n 10Conclusion
Linux offers a rich set of commands for file inspection. Knowing which tool fits the file size and required output—whether cat, tac, less, head, or tail —helps backend engineers troubleshoot efficiently without overloading system resources.
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.
