Master Fast Log Error Detection with Tail, Grep, and Sed Commands
This guide shows how to quickly pinpoint errors in massive log files on Linux by using dynamic viewing commands such as tail and head, extracting line numbers with grep, searching within time ranges using sed, counting matches, highlighting results, and navigating logs with pagination tools like more and less.
Quickly Locate Errors in Large Log Files
Use dynamic log viewing commands to inspect log files efficiently. tail -f catalina.out Open a log file from the beginning: cat catalina.out Redirect a portion of the log to a new file for focused analysis:
# cat -n catalina.out | grep 717892466 > nanjiangtest.txtSimple Tail/Head Command Usage
# tail -n number catalina.out # Show the last <em>number</em> lines
# tail -n +number catalina.out # Show all lines after <em>number</em>
# head -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>Method 1 – Find Line Numbers by Keyword
After obtaining a few matching lines with grep, retrieve surrounding context using the line numbers:
# cat -n catalina.out | grep "keyword"
# cat -n catalina.out | grep "keyword" | tail -n +13230539 | head -n 10"tail -n +13230539" queries logs after line 13230539.
"head -n 10" limits the result to the first 10 records.
Method 2 – Search Within a Specific Time Range
First verify that the desired time range exists in the current log file, then use grep or sed to extract it:
grep '11:07 18:29:20' catalina.out
grep '11:07 18:31:11' catalina.out sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.out
sed -n '/11:07 18:29:/,/11:07 18:31:/p' catalina.outMethod 3 – Count Occurrences of a Specific String
# grep '1175109632' catalina.out | wc -l
154Method 4 – Search the Last N Lines for a Keyword
# tail -n 20 catalina.out | grep 'INFO Takes:1'Method 5 – Highlight Keyword Results
# tail -n 20 catalina.out | grep 'INFO Takes:1' --colorMethod 6 – Highlight with Context Lines
# tail -n 20 catalina.out | grep 'INFO Takes:1' --color -A2Method 7 – Paginated View Using 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 (less)
Full‑screen navigation: Ctrl+F forward one screen, Ctrl+B back one screen, Ctrl+D forward half screen, Ctrl+U back half screen.
Line navigation: j forward one line, k back one line.
Other commands: G go to last line, g go to first line, q or ZZ quit.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
