Operations 10 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Log Viewing: Essential Commands for Efficient File Inspection

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
done

Direct 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.txt
cat -n test.txt

(show line numbers)

For very large files, avoid cat because it loads the whole file into memory.

Reverse Viewing with tac

tac

prints a file in reverse order (last line first). Example:

tac test.txt

Paging with more and less

more

provides 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

head

prints 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.txt
tail

prints 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.txt
tail -f test.txt

(live view)

Common Utilities

Combine commands with pipes ( |) to process output.

Example:

ll | head -n 3
grep

searches 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 10

With line numbers:

cat -n test.txt | head -n 20 | tail -n 10

Conclusion

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.

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.

command-lineFile 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.