Skip to content

Instantly share code, notes, and snippets.

@0xcafed00d
Created May 29, 2026 11:08
Show Gist options
  • Select an option

  • Save 0xcafed00d/045bba8946f2501510bdacd49c160fa3 to your computer and use it in GitHub Desktop.

Select an option

Save 0xcafed00d/045bba8946f2501510bdacd49c160fa3 to your computer and use it in GitHub Desktop.
Grep Cheat Sheet

grep Cheat Sheet

grep searches text using patterns.

grep [options] "pattern" file

Example:

grep "error" app.log

Finds lines containing error.


Basic searches

grep "hello" file.txt

Search for hello.

grep "hello" *.txt

Search all .txt files in the current directory.

grep "hello" file1.txt file2.txt

Search multiple files.

cat file.txt | grep "hello"

Search piped input.


Case sensitivity

grep -i "error" app.log

Case-insensitive search. Matches error, Error, ERROR.

grep "Error" app.log

Case-sensitive search.


Show line numbers

grep -n "error" app.log

Output includes line numbers.

Example output:

42:Database error occurred

Recursive search

grep -r "TODO" .

Search all files under the current directory.

grep -R "TODO" .

Like -r, but follows symbolic links.

grep -rn "TODO" src/

Recursive search with line numbers.

grep -r "main" ~/projects

Search inside a specific directory tree.


Match whole words

grep -w "cat" file.txt

Matches cat, but not category or concatenate.

grep -w "int" *.c

Find whole-word int in C files.


Match whole lines

grep -x "hello" file.txt

Matches only lines that are exactly:

hello

Invert match

grep -v "debug" app.log

Show lines that do not contain debug.

grep -vi "debug" app.log

Case-insensitive inverse match.

grep -v "^#" config.ini

Show lines that are not comments.

grep -v "^$" file.txt

Remove blank lines.


Count matches

grep -c "error" app.log

Count matching lines.

grep -ci "error" app.log

Count matching lines, ignoring case.

grep -rc "TODO" src/

Count matches per file recursively.


Show only matching text

grep -o "error" app.log

Print only the matched part, not the whole line.

grep -o "[0-9]\+" file.txt

Print numbers only.

grep -oE "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" file.txt

Extract email addresses.


List files with matches

grep -l "TODO" *.py

Show filenames that contain a match.

grep -rl "TODO" src/

Recursively list files containing TODO.

grep -L "TODO" *.py

Show files that do not contain a match.


Suppress filenames

grep -h "error" *.log

Do not show filenames in output.

Useful when searching multiple files but you only want matching lines.


Show filenames always

grep -H "error" app.log

Always show filename, even if searching one file.


Quiet mode

grep -q "error" app.log

Print nothing. Useful in scripts.

if grep -q "error" app.log; then
  echo "Errors found"
fi

Regular expressions

By default, grep uses basic regular expressions.

Start of line

grep "^ERROR" app.log

Matches lines starting with ERROR.

End of line

grep "done$" app.log

Matches lines ending with done.

Blank lines

grep "^$" file.txt

Find blank lines.

Lines with whitespace only

grep "^[[:space:]]*$" file.txt

Find empty or whitespace-only lines.

Any character

grep "h.t" file.txt

Matches hat, hot, hit, etc.

Character classes

grep "[aeiou]" file.txt

Matches lines containing a vowel.

grep "[0-9]" file.txt

Matches lines containing a digit.

grep "[A-Z]" file.txt

Matches lines containing an uppercase letter.

grep "[^0-9]" file.txt

Matches lines containing a non-digit.


Extended regex with -E

Use -E for easier regex syntax.

grep -E "cat|dog" file.txt

Matches cat or dog.

grep -E "error|failed|fatal" app.log

Search for multiple words.

grep -Ei "error|failed|fatal" app.log

Same, case-insensitive.

grep -E "colou?r" file.txt

Matches color or colour.

grep -E "[0-9]+" file.txt

Matches one or more digits.

grep -E "go+d" file.txt

Matches god, good, goood, etc.

grep -E "https?://" file.txt

Matches http:// or https://.


Multiple patterns

grep -e "error" -e "warning" app.log

Search for either error or warning.

grep -E "error|warning" app.log

Same using extended regex.

grep -f patterns.txt file.txt

Read patterns from a file.

Example patterns.txt:

error
warning
fatal

Context around matches

Lines after a match

grep -A 3 "Exception" app.log

Show matching line plus 3 lines after.

Lines before a match

grep -B 3 "Exception" app.log

Show 3 lines before the match.

Lines before and after

grep -C 3 "Exception" app.log

Show 3 lines before and after.

grep -nC 2 "panic" app.log

Show line numbers and 2 lines of context.


Search by file type

grep -r "TODO" --include="*.py" .

Search only Python files.

grep -r "TODO" --include="*.go" .

Search only Go files.

grep -r "TODO" --include="*.{c,h}" .

Search C source and header files.

grep -r "TODO" --exclude="*.min.js" .

Exclude minified JavaScript files.

grep -r "TODO" --exclude-dir=".git" .

Exclude .git.

grep -r "TODO" --exclude-dir="node_modules" .

Exclude node_modules.

grep -r "TODO" --exclude-dir="{.git,node_modules,dist,build}" .

Exclude several directories.


Search hidden files

grep -r "token" .

May include hidden files depending on shell expansion and directory recursion.

To search hidden files explicitly:

grep -r "token" .[^.]* *

Or better:

grep -r "token" . --exclude-dir=".git"

Literal strings with -F

Use -F when your pattern contains regex characters and you want them treated literally.

grep -F "user.name" file.txt

Matches literal user.name, not user + any char + name.

grep -F "[ERROR]" app.log

Matches literal [ERROR].

grep -F "a+b*c" file.txt

Matches literal a+b*c.


Perl-compatible regex with -P

Available in GNU grep, not always on macOS/BSD.

grep -P "\d+" file.txt

Matches digits.

grep -P "\bfoo\b" file.txt

Matches whole word foo.

grep -Po "id=\K[0-9]+" file.txt

Extracts numbers after id=.

Example input:

user id=123 active

Output:

123

Common practical examples

Find TODOs in code

grep -rn "TODO" src/

Find TODO, FIXME, or HACK

grep -rnE "TODO|FIXME|HACK" src/

Find errors in logs

grep -i "error" app.log

Find serious log messages

grep -Ei "error|fatal|panic|exception|failed" app.log

Show errors with context

grep -nC 3 -i "exception" app.log

Find lines that are not comments

grep -v "^#" config.conf

Find non-empty, non-comment lines

grep -Ev "^\s*($|#)" config.conf

Find IP addresses

grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log

Find URLs

grep -oE "https?://[^[:space:]]+" file.txt

Find email addresses

grep -oE "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" file.txt

Find hexadecimal numbers

grep -oE "0x[0-9A-Fa-f]+" file.txt

Find lines longer than 80 characters

grep -nE "^.{81,}$" file.txt

Find trailing whitespace

grep -nE "[[:space:]]+$" file.txt

Find tabs

grep -n $'\t' file.txt

Find CRLF line endings

grep -n $'\r' file.txt

Combining grep with other commands

Search command output

ps aux | grep "nginx"

Avoid matching the grep process itself

ps aux | grep "[n]ginx"

Search history

history | grep "docker"

Filter files from ls

ls -la | grep ".txt"

Search running ports

netstat -tulpn | grep ":8080"

or:

ss -tulpn | grep ":8080"

Search Docker containers

docker ps | grep "postgres"

Search Git history

git log --oneline | grep "fix"

Search Git diff

git diff | grep "^+"

Useful with find

find . -name "*.py" -exec grep -n "TODO" {} +

Search only Python files.

find . -type f -not -path "./.git/*" -exec grep -n "password" {} +

Search files while excluding .git.

find . -type f -name "*.log" -mtime -1 -exec grep -n "error" {} +

Search recent log files modified in the last day.


Exit codes

grep exit codes are useful in scripts:

Exit code Meaning
0 Match found
1 No match found
2 Error occurred

Example:

grep -q "success" output.log

if [ $? -eq 0 ]; then
  echo "Success found"
else
  echo "Success not found"
fi

Cleaner:

if grep -q "success" output.log; then
  echo "Success found"
fi

Color highlighting

grep --color=auto "error" app.log

Add this alias:

alias grep='grep --color=auto'

Binary files

grep "text" binaryfile

May print:

Binary file binaryfile matches

Force text mode:

grep -a "text" binaryfile

Ignore binary files:

grep -I "text" *

Null-delimited output

Useful for filenames with spaces.

grep -rlZ "TODO" . | xargs -0 sed -i 's/TODO/DONE/g'

Here:

grep -rlZ "TODO" .

Outputs matching filenames separated by null bytes.


Grep in compressed files

Use zgrep:

zgrep "error" app.log.gz

Case-insensitive:

zgrep -i "error" app.log.gz

Recursive compressed search usually requires find:

find . -name "*.gz" -exec zgrep -H "error" {} +

Grep variants

Command Purpose
grep Standard search
egrep Same as grep -E, mostly obsolete
fgrep Same as grep -F, mostly obsolete
zgrep Search compressed .gz files
ripgrep / rg Faster modern alternative
ag The Silver Searcher, code-search tool

grep vs rg

If you have ripgrep installed:

rg "TODO"

It recursively searches by default and respects .gitignore.

Equivalent-ish grep:

grep -rn "TODO" .

Search only Go files:

rg -t go "TODO"

With grep:

grep -rn --include="*.go" "TODO" .

Handy aliases

alias grep='grep --color=auto'
alias grepi='grep -i'
alias grepn='grep -n'
alias grepr='grep -rn'
alias grepc='grep -nC 3'

Most useful options

Option Meaning
-i Ignore case
-n Show line numbers
-r Recursive search
-R Recursive and follow symlinks
-w Match whole words
-x Match whole lines
-v Invert match
-c Count matching lines
-l List files with matches
-L List files without matches
-o Print only matched text
-h Hide filenames
-H Always show filenames
-q Quiet mode
-E Extended regex
-F Fixed string / literal search
-P Perl regex, GNU grep only
-A N Show N lines after
-B N Show N lines before
-C N Show N lines before and after
--include Include matching filenames
--exclude Exclude matching filenames
--exclude-dir Exclude directories
--color=auto Highlight matches

Quick recipes

grep -rn "pattern" .

Search recursively with line numbers.

grep -rni "pattern" .

Recursive, line numbers, case-insensitive.

grep -rnE "foo|bar|baz" .

Search multiple patterns.

grep -rn --include="*.go" "pattern" .

Search only Go files.

grep -rn --exclude-dir=".git" --exclude-dir="node_modules" "pattern" .

Search while skipping noisy directories.

grep -v "^#" file.conf | grep -v "^$"

Show non-comment, non-empty lines.

grep -Ev "^\s*($|#)" file.conf

Same, cleaner.

grep -oE "[0-9]+" file.txt

Extract numbers.

grep -q "pattern" file.txt && echo "found"

Check if a file contains a pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment