Quick Techniques to Pinpoint Errors in Massive Log Files
Learn how to efficiently navigate, filter, and extract specific entries from large log files using tail, head, grep, sed, and pagination commands, with practical examples for locating error lines and time ranges.
Dynamic Log Viewing
Use tail -f catalina.out to follow a log file in real time.
Open Log from Start
Display the entire file with cat catalina.out or redirect output to a new file for further analysis:
# cat -n catalina.out | grep 717892466 > nanjiangtest.txtBasic tail/head Commands
tail -n number catalina.out– show the last number lines. tail -n +number catalina.out – display from line number onward. head -n number catalina.out – show the first number lines. head -n -number catalina.out – show all but the last number lines.
Method 1: Find Line Numbers by Keyword
First obtain the line number containing a keyword, then extract surrounding lines:
# cat -n catalina.out | grep "keyword"
# cat -n catalina.out | tail -n +13230539 | head -n 10Method 2: Search Within a Time Range
Use grep to verify timestamps, then extract the range with sed:
grep '11:07 18:29:20' catalina.out
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 -l
154Method 4: Search the Last n Lines for a Keyword
# tail -n 20 catalina.out | grep 'INFO Takes:1'Method 5: Highlight Matches
# tail -n 20 catalina.out | grep 'INFO Takes:1' --colorMethod 6: Show Context Lines Around Matches
# tail -n 20 catalina.out | grep 'INFO Takes:1' --color -a2Method 7: Paginated Viewing
Pipe the filtered output to more or less for page‑by‑page navigation:
# 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 (within less/more)
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 – quit the pager
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.
