📖 Guide
Grep — Complete Reference
Comprehensive grep cheat sheet covering basic search, regex patterns, context options, file filtering, recursive search, and common patterns.
54 commands across 8 categories
Basic SearchRegex PatternsContext OptionsFile FilteringCounting & ListingRecursive SearchExtended RegexCommon Patterns
Basic Search
| Command | Description |
|---|---|
grep 'pattern' file | Search for pattern in a file |
grep -i 'pattern' file | Case-insensitive search |
grep -v 'pattern' file | Invert match: print lines NOT matching pattern |
grep -w 'word' file | Match whole words only |
grep -x 'exact line' file | Match entire lines only |
grep -n 'pattern' file | Show line numbers with matches |
grep --color=auto 'pattern' file | Highlight matches in color |
command | grep 'pattern'e.g. ps aux | grep nginx | Filter output of another command |
Regex Patterns
| Command | Description |
|---|---|
grep '^start' file | Match lines starting with 'start' |
grep 'end$' file | Match lines ending with 'end' |
grep '^$' file | Match empty lines |
grep 'col.r' file | Dot matches any single character (color, colour, etc.) |
grep 'ab*c' file | Match 'a', zero or more 'b's, then 'c' |
grep '[0-9]\{3\}' file | Match exactly 3 digits (BRE syntax) |
grep '[aeiou]' file | Match any vowel (character class) |
grep '[^0-9]' file | Match any non-digit character |
Context Options
| Command | Description |
|---|---|
grep -A 3 'pattern' file | Show 3 lines after each match |
grep -B 3 'pattern' file | Show 3 lines before each match |
grep -C 3 'pattern' file | Show 3 lines before and after each match |
grep -m 5 'pattern' file | Stop after first 5 matches |
grep -o 'pattern' filee.g. grep -oP '\d+\.\d+\.\d+' version.txt | Print only the matching part, not the whole line |
File Filtering
| Command | Description |
|---|---|
grep 'pattern' *.txt | Search in all .txt files in current directory |
grep --include='*.js' -r 'pattern' . | Recursively search only in .js files |
grep --exclude='*.log' -r 'pattern' . | Recursively search, skipping .log files |
grep --exclude-dir=node_modules -r 'pattern' . | Skip entire directories during recursive search |
grep -r --include='*.{js,ts}' 'TODO' src/ | Search multiple file types in a directory |
grep -rL 'pattern' . | List files that do NOT contain the pattern |
Counting & Listing
| Command | Description |
|---|---|
grep -c 'pattern' file | Count matching lines (not matches) per file |
grep -l 'pattern' *.txt | List only filenames containing the pattern |
grep -L 'pattern' *.txt | List only filenames NOT containing the pattern |
grep -c '' file | Count total lines in a file (like wc -l) |
grep -o 'pattern' file | wc -l | Count total number of matches (not lines) |
grep -rch 'pattern' . | awk '{sum+=$1} END {print sum}' | Count total matches across all files recursively |
Recursive Search
| Command | Description |
|---|---|
grep -r 'pattern' . | Search recursively in all files from current directory |
grep -rn 'pattern' . | Recursive search with line numbers |
grep -rl 'pattern' /path/ | List files containing pattern under a path |
grep -rI 'pattern' . | Recursive search, skip binary files |
grep -r --exclude-dir={.git,node_modules} 'pattern' . | Skip multiple directories |
grep -rZ 'pattern' . | xargs -0 sed -i 's/pattern/replace/g' | Find and replace across files (null-delimited for safety) |
Extended Regex
| Command | Description |
|---|---|
grep -E 'pattern1|pattern2' file | Match either pattern (alternation, no escaping needed) |
grep -E '^(yes|no)$' file | Group with parentheses and alternation |
grep -E '[0-9]{2,4}' file | Match 2 to 4 consecutive digits |
grep -E '(ab)+' file | Match one or more repetitions of 'ab' |
grep -P '\d{3}-\d{4}' filee.g. Matches patterns like 555-1234 | Perl-compatible regex with \d shorthand |
grep -P '(?<=@)\w+' -o file | Perl regex with lookbehind (extract domain after @) |
grep -E '^[[:alpha:]]+$' file | POSIX character class: lines with only letters |
Common Patterns
| Command | Description |
|---|---|
grep -E '^\s*$' file | Match blank or whitespace-only lines |
grep -P '\b\d{1,3}(\.\d{1,3}){3}\b' file | Match IPv4 addresses |
grep -oP '[\w.+-]+@[\w-]+\.[\w.]+' file | Extract email addresses |
grep -oP 'https?://[^\s"]+' file | Extract URLs from text |
grep -v '^#' file | grep -v '^$' | Show file without comments or blank lines |
grep -c 'ERROR' /var/log/*.log | sort -t: -k2 -rn | Count errors per log file, sorted by count descending |
grep -rn 'TODO\|FIXME\|HACK' src/ | Find code annotations across a project |
history | grep 'ssh' | Search command history for SSH commands |
📖 Free, searchable command reference. Bookmark this page for quick access.