Master Fast Log Error Detection with Tail, Grep, and Sed Commands
This guide shows how to quickly locate errors in massive log files using Linux commands such as tail, head, grep, and sed, offering multiple techniques—including line‑number extraction, time‑range queries, match counting, colored highlights, and pagination—to streamline debugging and log analysis.
Quick Log Viewing
Use tail -f catalina.out to continuously monitor a log file as new entries are written.
Open a log from the beginning with cat catalina.out.
Basic Tail/Head Commands
tail -n number catalina.out– display the last number lines. tail -n +number catalina.out – show all lines starting from line number . head -n number catalina.out – display the first number lines. head -n -number catalina.out – display all lines except the last number lines.
Method 1 – Find Line Numbers by Keyword
First locate the line numbers containing a keyword, then view surrounding lines:
cat -n catalina.out | grep "keyword"
# Example output shows line numbers like 13230539Combine the line number with tail and head to extract a specific range:
cat -n catalina.out | tail -n +13230539 | head -n 10Explanation: tail -n +13230539 – start from line 13,230,539. head -n 10 – then keep only the first 10 lines of that result.
Method 2 – Query Logs Within a Time Range
First verify that the timestamps exist in the current log file, then use grep or sed to extract the interval:
grep '11:07 18:29:20' catalina.out
grep '11:07 18:31:11' catalina.outExtract the block between the two timestamps with sed:
sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.outMethod 3 – Count Occurrences of a Specific String
Count how many times a pattern appears:
grep '1175109632' catalina.out | wc -l
# Output: 154Method 4 – Search the Last N Lines for a Keyword
Combine tail with grep to find a keyword in the most recent lines:
tail -n 20 catalina.out | grep 'INFO Takes:1'Method 5 – Highlight Keyword in the Last N Lines
Use --color to color‑highlight matches:
tail -n 20 catalina.out | grep 'INFO Takes:1' --colorMethod 6 – Show Context Around a Match
Display two lines of context before and after each match:
tail -n 20 catalina.out | grep 'INFO Takes:1' --color -A2Method 7 – Paginate Results with more or less
Pipe the filtered output to a pager for easier navigation:
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -A2 | more
# or
tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -A2 | lessNavigation Shortcuts in less
Ctrl+F– move forward one screen. Ctrl+B – move backward one screen. Ctrl+D – move forward half a screen. Ctrl+U – move backward half a screen. j – move forward one line. k – move backward one line. G – jump to the end of the file. g – jump to the beginning of the file. q or ZZ – exit the pager.
These commands enable efficient navigation and inspection of large log files, helping developers quickly pinpoint errors and understand system behavior.
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.
