Master Fast Log Error Detection with Tail, Grep, and Sed Commands
This guide shows how to quickly locate errors in massive server logs by using Linux commands such as tail, cat, grep, sed, head, and pagination tools, offering multiple practical techniques for pinpointing problematic entries.
Why Log Analysis Matters
Viewing server and application logs is an essential skill for every programmer because logs are the best way to identify where errors occur. When a server crashes, knowing how to locate the error quickly can save you from a lot of trouble.
Dynamic Log Viewing
tail -f catalina.outContinuously follows the end of the log file, showing new entries as they appear.
Open Log from the Beginning
cat catalina.outDisplays the entire content of the log file.
Redirect Output to a New File
cat -n catalina.out | grep "keyword" > nanjiangtest.txtSearches for a keyword, shows line numbers, and saves the result to nanjiangtest.txt for further inspection.
Simple Tail/Head Commands
# Show the last <em>number</em> lines
tail -n number catalina.out
# Show lines after line <em>number</em>
tail -n +number catalina.out
# Show the first <em>number</em> lines
head -n number catalina.out
# Show all but the last <em>number</em> lines
head -n -number catalina.outMethod 1 – Find Line Numbers by Keyword
First obtain the line numbers of the relevant entries, then view surrounding lines. cat -n test.log | grep "keyword" After getting the line number, you can extract a range:
cat -n catalina.out | tail -n +13230539 | head -n 10Method 2 – Search Within a Time Range
# Search for a specific timestamp
grep '11:07 18:29:20' catalina.out
# Use sed to extract a range between two timestamps
sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.outMethod 3 – Count Occurrences of a String
grep '1175109632' catalina.out | wc -lThis returns the number of matching lines.
Method 4 – Tail Last number Lines and Grep 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
tail -n 20 catalina.out | grep 'INFO Takes:1' --color -a2The -a2 option displays two lines of context before and after each match.
Method 7 – Paginate Results
# Use more for simple pagination
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -a2 | more
# Use less for more control
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -a2 | lessAdditional Navigation Shortcuts (in less )
ctrl + F– move forward one screen ctrl + B – move backward one screen ctrl + D – move forward half screen ctrl + U – move 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
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.
