Master Log Analysis: Fast Linux Commands to Pinpoint Errors
This guide shows programmers how to quickly locate errors in massive server logs using essential Linux commands such as tail, cat, grep, sed, and pagination tools, providing step‑by‑step examples and tips for efficient debugging.
Why Log Analysis Matters
Viewing server and application logs is a fundamental skill for developers because logs are the most reliable way to identify where errors occur. When a server crashes, knowing how to pinpoint the problem can save you from taking the blame.
Quick Ways to View Logs Dynamically
tail -f catalina.outContinuously follows the end of the log file. cat catalina.out Displays the entire log from the beginning.
You can redirect output to a new file for focused inspection:
cat -n catalina.out | grep "717892466" > nanjiangtest.txtBasic tail / head Commands
# Show the last <em>number</em> lines
tail -n number catalina.out
# Show from line <em>number</em> to the end
tail -n +number catalina.out
# Show the first <em>number</em> lines
head -n number catalina.out
# Show all lines except the last <em>number</em>
head -n -number catalina.outMethod 1 – Find Line Numbers with grep
First obtain the line numbers containing a keyword, then view surrounding lines: cat -n test.log | grep "keyword" Example output (line numbers highlighted):
# cat -n catalina.out | grep 717892466
13230539 [11:07 17:47:11] INFO nanjiang:Edit Old Article:717892466-2020-11-07 17:47:11
13230593 [11:07 17:47:15] INFO nanjiang Save Article ID IS:717892466
...Then combine with tail and head to see a context window:
cat -n catalina.out | tail -n +13230539 | head -n 10Method 2 – Search Within a Time Range
grep '11:07 18:29:20' catalina.out
grep '11:07 18:31:11' catalina.outOr use sed to extract a block between two timestamps:
sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.outMethod 3 – Count Occurrences of a Specific String
grep '1175109632' catalina.out | wc -lResult:
154Method 4 – Search the Last number Lines for a Keyword
tail -n 20 catalina.out | grep 'INFO Takes:1'Method 5 – Highlight Matches with Color
tail -n 20 catalina.out | grep 'INFO Takes:1' --colorMethod 6 – Show Matching Lines with Two Context Lines
tail -n 20 catalina.out | grep 'INFO Takes:1' --color -A2Method 7 – Paginated Viewing with more / less
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -A2 | more
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -A2 | lessAdditional Navigation Shortcuts (for less )
ctrl + F– forward one screen ctrl + B – backward one screen ctrl + D – forward half screen ctrl + U – backward half screen j – move down one line k – move up one line G – go to the last line g – go to the first line q or ZZ – exit
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.
