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.
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>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>Search only JS/TS files
grep -R --include="*.js" --include="*.jsx" --include="*.ts" --include="*.tsx" -n "<pattern>" srcExclude 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" srcContainer 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"Environment-style key match
grep -E "^[A-Z_][A-Z0-9_]*=" .envIPv4 pattern search
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" access.logHTTP methods in logs
grep -E "GET|POST|PUT|PATCH|DELETE" access.logOnly matched token output
grep -oE "[A-Z]{3}-[0-9]{4}" <file>