Advanced Grep Techniques: Searching Like a Pro

Get faster and more precise searches with these practical flags and patterns.

Regex Superpowers

Grep understands basic regular expressions by default. Use -E for extended regex and -P for PCRE if available.

grep -E "warn|error|fail" app.log

Context is King

Show surrounding lines with -C for context, -A after, -B before.

grep -C2 "timeout" /var/log/nginx/error.log

Recursive on Codebases

Search whole trees with -R and exclude noise.

grep -R --exclude-dir=".git" --exclude="*.min.js" "TODO" .

Only Filenames or Only Matches

Speed up scans with -l to list files or extract just the match with -o.

grep -Rl "API_KEY" .
grep -oE "https?://[^ ]+" README.md

Case and Word Boundaries

grep -iw "admin" users.txt

Binary and Color

Ignore binary files with -I. Enable color with --color=auto for easier eyeballing.

Pipelines and Process Substitution

journalctl -u nginx | grep -i "error"
grep -E "5..|timeout" <(curl -sI https://example.com)

Performance Tips

The VPN Index - Find the best VPN for your needs