Operations 12 min read

Master Log Analysis: Fast Linux Commands to Pinpoint Errors

This guide shows programmers how to quickly locate errors in massive server logs using essential Linux commands such as tail, cat, grep, sed, and pagination tools, providing step‑by‑step examples and tips for efficient debugging.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Log Analysis: Fast Linux Commands to Pinpoint Errors

Why Log Analysis Matters

Viewing server and application logs is a fundamental skill for developers because logs are the most reliable way to identify where errors occur. When a server crashes, knowing how to pinpoint the problem can save you from taking the blame.

Quick Ways to View Logs Dynamically

tail -f catalina.out

Continuously follows the end of the log file. cat catalina.out Displays the entire log from the beginning.

You can redirect output to a new file for focused inspection:

cat -n catalina.out | grep "717892466" > nanjiangtest.txt

Basic tail / head Commands

# Show the last <em>number</em> lines
tail -n number catalina.out
# Show from line <em>number</em> to the end
tail -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>
head -n -number catalina.out

Method 1 – Find Line Numbers with grep

First obtain the line numbers containing a keyword, then view surrounding lines: cat -n test.log | grep "keyword" Example output (line numbers highlighted):

# cat -n catalina.out | grep 717892466
13230539   [11:07 17:47:11] INFO nanjiang:Edit Old Article:717892466-2020-11-07 17:47:11
13230593   [11:07 17:47:15] INFO nanjiang Save Article ID IS:717892466
...

Then combine with tail and head to see a context window:

cat -n catalina.out | tail -n +13230539 | head -n 10

Method 2 – Search Within a Time Range

grep '11:07 18:29:20' catalina.out
grep '11:07 18:31:11' catalina.out

Or use sed to extract a block between two timestamps:

sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.out

Method 3 – Count Occurrences of a Specific String

grep '1175109632' catalina.out | wc -l

Result:

154

Method 4 – Search the Last number Lines for 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' --color

Method 6 – Show Matching Lines with Two Context Lines

tail -n 20 catalina.out | grep 'INFO Takes:1' --color -A2

Method 7 – Paginated Viewing with 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 | less

Additional 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 – exit

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingOperationsLinuxlog analysisShell Commands
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.