← 개발일지

How to Search Logs with grep in Linux


Problem

You have multiple log files and need to quickly find all entries containing a specific ID like:

AUM0000058654

Your files look like:

pc_2026-01-16.1.log
pc_2026-01-16.error.log
pc_2026-01-16.backup.log


Basic Solution: grep

grep "AUM0000058654" pc_2026-01-16.*.log

How it actually works

  • pc_2026-01-16.*.log
    → Expanded by the shell (globbing), not grep
  • grep
    → Reads each file and checks every line for a match

Execution flow:

Expand files → Read line-by-line → Match string → Print results


Must-know Options for Real Use

Show line numbers

grep -n "AUM0000058654" pc_2026-01-16.*.log

Helps pinpoint exact positions.


Include surrounding context

grep -n -C 3 "AUM0000058654" pc_2026-01-16.*.log

Essential for debugging root causes.


Handle large outputs safely

grep -n "AUM0000058654" pc_2026-01-16.*.log | less

Lets you scroll and search interactively.


List matching files only

grep -l "AUM0000058654" pc_2026-01-16.*.log

Useful as a first filter step.


Common Confusion: glob vs regex

|Concept|Meaning| |---|---| |*|File matching (shell globbing)| |regex|Pattern matching inside grep|

Important:
* in filenames is handled by the shell, not grep.


Real-world Usage Insight

IDs like AUM0000058654 are usually:

  • Request IDs
  • Transaction IDs

Using them, you can:

  • Trace request flows
  • Identify failure points
  • Analyze latency and bottlenecks

Recommended Command

grep -n -C 2 "AUM0000058654" pc_2026-01-16.*.log | less

This gives you:

  • Exact location
  • Context around the event
  • Safe navigation

Takeaway

  • grep is not just search — it's a trace tool
  • globbing and regex operate at different layers
  • effective debugging = context + pattern + flow