Grep Cheatsheet | Search Commands for Logs and Code

Grep Cheatsheet

Search logs and code fast with beginner-to-advanced grep patterns.

22/22 commands
TerminalAll Developers

Grep is one of the most important developer commands for code search and incident debugging. This cheatsheet includes practical day-to-day and advanced query patterns.

Core Search Patterns

Recursive string search
grep -R "<text>" .
Case-insensitive search
grep -Ri "<text>" .
Show line numbers
grep -Rn "<text>" .
Match whole word only
grep -Rw "<word>" .
OR pattern with extended regex
grep -E "error|warn|fatal" <file>

Context, Count, and File Results

Show 3 lines before and after
grep -nC 3 "<pattern>" <file>
Show 5 lines after a match
grep -nA 5 "<pattern>" <file>
Show 2 lines before a match
grep -nB 2 "<pattern>" <file>
Count matches per file
grep -Rc "<pattern>" <path>
List only filenames with matches
grep -Rl "<pattern>" <path>

Targeted Search in Large Repos

Search only JS/TS files
grep -R --include="*.js" --include="*.jsx" --include="*.ts" --include="*.tsx" -n "<pattern>" src
Exclude node_modules and dist
grep -R --exclude-dir=node_modules --exclude-dir=dist -n "<pattern>" .
Exclude minified files
grep -R --exclude="*.min.js" -n "<pattern>" .
Search multiple patterns
grep -R -n -e "TODO" -e "FIXME" src

Logs and Pipeline Debugging

Container logs (error class)
docker logs <container> 2>&1 | grep -Ei "error|timeout|exception|fatal"
Systemd service logs
journalctl -u <service> -n 500 | grep -Ei "panic|fatal|oom|refused"
Search across log files safely
find . -name "*.log" -print0 | xargs -0 grep -n "<pattern>"
Process lookup without matching grep itself
ps aux | grep "[n]ode"

Useful Regex Patterns

Environment-style key match
grep -E "^[A-Z_][A-Z0-9_]*=" .env
IPv4 pattern search
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log
HTTP methods in logs
grep -E "GET|POST|PUT|PATCH|DELETE" access.log
Only matched token output
grep -oE "[A-Z]{3}-[0-9]{4}" <file>

What Is Grep Cheatsheet?

Grep is one of the most important developer commands for code search and incident debugging. This cheatsheet includes practical day-to-day and advanced query patterns.

Recursive and regex search examples

Context, count, and file-scoped matching

Pipeline-ready log filtering commands

Key Features of Grep Cheatsheet

Recursive and regex search examples

Context, count, and file-scoped matching

Pipeline-ready log filtering commands

How to Use Grep Cheatsheet

Step 1

Start with recursive searches for quick codebase scans

Step 2

Use include and exclude flags to reduce noise in large projects

Step 3

Apply context and pipeline queries during log debugging and incidents

Still need help?

Need a hand with Grep Cheatsheet? Reach out through support or send a feature request with your workflow.

Contact support