Skip to main content

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.

Updated: 2026-07-16·33 commands

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" `

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)

CommandLevel
grep "error" app.log
Search for string in file (case-sensitive)
Basic
grep -i "error" app.log
Case-insensitive search
Basic
grep -n "error" app.log
Show line numbers of matches
Basic
grep -v "debug" app.log
Invert match, show lines not containing pattern
Basic
grep -w "error" app.log
Match whole words only
Basic
grep -x "ERROR: Connection timeout" app.log
Match whole lines only
Intermediate
grep -l "error" *.log
Show only filenames with matches
Basic
grep -L "error" *.log
Show filenames without matches
Intermediate
grep -q "pattern" file && echo "Found"
Quiet mode, check existence via exit code
Intermediate
grep -f patterns.txt target.log
Read search patterns from a file
Intermediate

Output(7)

CommandLevel
grep -c "error" app.log
Count matching lines
Basic
grep -o "[0-9]\\+" app.log
Output only the matched part of the line
Intermediate
grep -m 5 "error" app.log
Limit number of matches
Intermediate
grep --color=always "pattern" file
Highlight matched content
Basic
grep -H "pattern" file1 file2
Force display of filenames
Basic
grep -h "pattern" file1 file2
Suppress filename display
Basic
grep -rlZ "pattern" . | xargs -0 rm
Null-delimited output for handling special filenames with xargs -0
Expert

Recursive(3)

CommandLevel
grep -r "TODO" .
Recursively search current directory
Basic
grep -R "TODO" /etc/
Recursively search and follow symlinks
Intermediate
grep -rc "import" src/
Recursively count matches per file
Intermediate

Regex(4)

CommandLevel
grep -e "ERROR" -e "FATAL" app.log
Search for multiple patterns (logical OR)
Intermediate
grep -E "[0-9]{3}-[0-9]{4}" phone.txt
Use extended regular expressions (ERE)
Intermediate
grep -P "\\d{3}-\\d{4}" phone.txt
Use Perl-compatible regular expressions (PCRE)
Expert
grep -E "cat|dog|bird" animals.txt
Logical OR matching multiple patterns
Intermediate

Context(3)

CommandLevel
grep -A 2 "Exception" stacktrace.log
Show 2 lines after matching line
Basic
grep -B 2 "ERROR" app.log
Show 2 lines before matching line
Basic
grep -C 3 "timeout" app.log
Show 3 lines before and after matching line
Basic

File Filter(6)

CommandLevel
grep -r --include="*.py" "class" .
Search only Python files
Intermediate
grep -r --exclude="*.min.js" "function" .
Exclude files matching a glob
Intermediate
grep -r --exclude-dir=node_modules "require" .
Exclude specified directories
Intermediate
grep -r --exclude-dir={node_modules,.git,dist} "pattern" .
Exclude multiple directories
Intermediate
grep -I "text" binary_file
Ignore binary files
Intermediate
grep -a "text" binary.exe
Search binary files (treat as text)
Expert

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-16.