Grep Command Cheatsheet
This cheatsheet covers the most commonly used grep, egrep, and fgrep operations, including basic search, recursive lookup, context viewing, regex matching, output formatting, and file filtering for daily text search and log analysis.
Basic Operations
Basic search syntax and common options.
``bash
# Search for string in file (case-sensitive)
grep "error" app.log
# Case-insensitive search grep -i "error" app.log
# Show line numbers grep -n "error" app.log
# Invert match (show non-matching lines) grep -v "debug" app.log
# Match whole words only grep -w "error" app.log
# Match whole lines only grep -x "ERROR: Connection timeout" app.log
# Count matching lines grep -c "error" app.log
# Show only filenames with matches grep -l "error" *.log
# Show filenames without matches grep -L "error" *.log
# Output only matched parts grep -o "[0-9]\\+" app.log
# Limit number of matches grep -m 5 "error" app.log
# Quiet mode (exit code only)
grep -q "pattern" file && echo "Found"
`
Recursive Search
Search recursively through directory trees.
`bash
# Recursive search in current directory
grep -r "TODO" .
# Recursive search (follow symlinks) grep -R "TODO" /etc/
# Show filenames only grep -rl "function" src/
# Show filenames without matches (inverse) grep -rL "error" logs/
# Read patterns from file grep -f patterns.txt target.log
# Count matches per file grep -rc "import" src/
# Search multiple patterns grep -e "ERROR" -e "FATAL" app.log
# Combine with find
find . -name "*.py" -exec grep "def " {} +
`
Context Control
Display context around matching lines.
`bash
# Show 2 lines after match
grep -A 2 "Exception" stacktrace.log
# Show 2 lines before match grep -B 2 "ERROR" app.log
# Show 3 lines before and after grep -C 3 "timeout" app.log
# Group results with -- separator grep -C 2 "error" log.txt
# Highlight matches
grep --color=always "pattern" file
`
Regular Expressions
Using extended and Perl-compatible regular expressions.
`bash
# Extended regular expressions (ERE)
grep -E "[0-9]{3}-[0-9]{4}" phone.txt
# Perl-compatible regex (PCRE) grep -P "\\d{3}-\\d{4}" phone.txt
# Start of line grep "^ERROR" app.log
# End of line grep "done$" build.log
# Character class grep -E "gr[ae]y" colors.txt
# Any single character grep "f..l" words.txt
# Repetition grep -E "go+" pattern.txt
# Optional character grep -E "colou?r" colors.txt
# Specific count grep -E "\\d{3,5}" numbers.txt
# Word boundary grep -E "\\berror\\b" text.txt
# Logical OR grep -E "cat|dog|bird" animals.txt
# Ignore binary files
grep -I "text" binary_file
`
Output Control
Formatting and managing grep output.
`bash
# Show filename (default with multiple files)
grep -H "pattern" file1 file2
# Suppress filename grep -h "pattern" file1 file2
# Null-delimited output (for xargs -0) grep -rlZ "pattern" . | xargs -0 rm
# Colored output (always/auto/never) grep --color=auto "pattern" file
# Quiet mode, check existence only grep -q "pattern" file && echo "exists"
# Aligned output with tab separator grep -n "pattern" file
# Pipe to other commands grep "error" app.log | cut -d' ' -f1
# Chain multiple greps grep "ERROR" app.log | grep -v "timeout" | head -20
# Search command history history | grep "docker"
# Combine with wc grep -c "pattern" file.log
# Combine with sort and uniq
grep -o "\\b[A-Z]\\+\\b" file | sort | uniq -c | sort -rn
`
File Filtering
Control which files and directories grep searches.
`bash
# Search only specific file types
grep -r --include="*.py" "class" .
# Include multiple file types grep -r --include="*.{js,ts}" "import" .
# Exclude specific files grep -r --exclude="*.min.js" "function" .
# Exclude files listed in a file grep -r --exclude-from=ignore.txt "pattern" .
# Exclude directories grep -r --exclude-dir=node_modules "require" .
# Exclude multiple directories grep -r --exclude-dir={node_modules,.git,dist} "pattern" .
# Search only matching glob grep -r --include="*.log" "ERROR" /var/log/
# Search binary files (show text portions) grep -a "text" binary.exe
# Skip binary files grep -rI "pattern" . ``
Basic(10)
| Command | Level | ||
|---|---|---|---|
grep "error" app.logSearch for string in file (case-sensitive) | Basic | grep "error" app.log | |
grep -i "error" app.logCase-insensitive search | Basic | grep -i "error" app.log | |
grep -n "error" app.logShow line numbers of matches | Basic | grep -n "error" app.log | |
grep -v "debug" app.logInvert match, show lines not containing pattern | Basic | grep -v "debug" app.log | |
grep -w "error" app.logMatch whole words only | Basic | grep -w "error" app.log | |
grep -x "ERROR: Connection timeout" app.logMatch whole lines only | Intermediate | grep -x "ERROR: Connection timeout" app.log | |
grep -l "error" *.logShow only filenames with matches | Basic | grep -l "error" *.log | |
grep -L "error" *.logShow filenames without matches | Intermediate | grep -L "error" *.log | |
grep -q "pattern" file && echo "Found"Quiet mode, check existence via exit code | Intermediate | grep -q "pattern" file && echo "Found" | |
grep -f patterns.txt target.logRead search patterns from a file | Intermediate | grep -f patterns.txt target.log |
Output(7)
| Command | Level | ||
|---|---|---|---|
grep -c "error" app.logCount matching lines | Basic | grep -c "error" app.log | |
grep -o "[0-9]\\+" app.logOutput only the matched part of the line | Intermediate | grep -o "[0-9]\\+" app.log | |
grep -m 5 "error" app.logLimit number of matches | Intermediate | grep -m 5 "error" app.log | |
grep --color=always "pattern" fileHighlight matched content | Basic | grep --color=always "pattern" file | |
grep -H "pattern" file1 file2Force display of filenames | Basic | grep -H "pattern" file1 file2 | |
grep -h "pattern" file1 file2Suppress filename display | Basic | grep -h "pattern" file1 file2 | |
grep -rlZ "pattern" . | xargs -0 rmNull-delimited output for handling special filenames with xargs -0 | Expert | grep -rlZ "pattern" . | xargs -0 rm |
Recursive(3)
| Command | Level | ||
|---|---|---|---|
grep -r "TODO" .Recursively search current directory | Basic | grep -r "TODO" . | |
grep -R "TODO" /etc/Recursively search and follow symlinks | Intermediate | grep -R "TODO" /etc/ | |
grep -rc "import" src/Recursively count matches per file | Intermediate | grep -rc "import" src/ |
Regex(4)
| Command | Level | ||
|---|---|---|---|
grep -e "ERROR" -e "FATAL" app.logSearch for multiple patterns (logical OR) | Intermediate | grep -e "ERROR" -e "FATAL" app.log | |
grep -E "[0-9]{3}-[0-9]{4}" phone.txtUse extended regular expressions (ERE) | Intermediate | grep -E "[0-9]{3}-[0-9]{4}" phone.txt | |
grep -P "\\d{3}-\\d{4}" phone.txtUse Perl-compatible regular expressions (PCRE) | Expert | grep -P "\\d{3}-\\d{4}" phone.txt | |
grep -E "cat|dog|bird" animals.txtLogical OR matching multiple patterns | Intermediate | grep -E "cat|dog|bird" animals.txt |
Context(3)
| Command | Level | ||
|---|---|---|---|
grep -A 2 "Exception" stacktrace.logShow 2 lines after matching line | Basic | grep -A 2 "Exception" stacktrace.log | |
grep -B 2 "ERROR" app.logShow 2 lines before matching line | Basic | grep -B 2 "ERROR" app.log | |
grep -C 3 "timeout" app.logShow 3 lines before and after matching line | Basic | grep -C 3 "timeout" app.log |
File Filter(6)
| Command | Level | ||
|---|---|---|---|
grep -r --include="*.py" "class" .Search only Python files | Intermediate | grep -r --include="*.py" "class" . | |
grep -r --exclude="*.min.js" "function" .Exclude files matching a glob | Intermediate | grep -r --exclude="*.min.js" "function" . | |
grep -r --exclude-dir=node_modules "require" .Exclude specified directories | Intermediate | grep -r --exclude-dir=node_modules "require" . | |
grep -r --exclude-dir={node_modules,.git,dist} "pattern" .Exclude multiple directories | Intermediate | grep -r --exclude-dir={node_modules,.git,dist} "pattern" . | |
grep -I "text" binary_fileIgnore binary files | Intermediate | grep -I "text" binary_file | |
grep -a "text" binary.exeSearch binary files (treat as text) | Expert | grep -a "text" binary.exe |