Quick Techniques for Locating Errors in Large Log Files Using Linux Commands
This article demonstrates several efficient Linux command‑line methods—including tail, head, grep, sed, and pagination tools—to quickly pinpoint error entries, view surrounding context, filter by time range, and count occurrences within massive log files.
The author, an architect who writes code and poetry, shares practical ways to quickly locate errors in large log files.
Dynamic log viewing : Use tail -f catalina.out to follow a log in real time, or cat catalina.out to display the whole file. You can also redirect a specific log segment to a file with cat -n catalina.out | grep 717892466 > nanjiangtest.txt .
Simple tail/head commands :
tail -n number catalina.out # show the last *number* lines
tail -n +number catalina.out # show 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* linesMethod 1 – Find line numbers then extract surrounding lines :
First obtain the line number of the keyword with cat -n test.log | grep "关键词" . Then retrieve context, for example:
cat -n catalina.out | tail -n +13230539 | head -n 10Method 2 – Query a specific time range :
grep '11:07 18:29:20' catalina.out
grep '11:07 18:31:11' catalina.outOr use sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.out to extract the whole interval.
Method 3 – Count occurrences of a string :
grep '1175109632' catalina.out | wc -lMethod 4 – Show the last *N* lines and filter 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 matches with surrounding lines (two lines of context):
tail -n 20 catalina.out | grep 'INFO Takes:1' --color -a2Method 7 – Paginate results using more or 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 – quit
The article concludes with an invitation to join a community of architects and a reminder that sharing the post provides strong support.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.