grep Commands

grep commands cheatsheet.

Search files and command output fast — recursively, case-insensitively, with regex. Tap any command to copy it.

Basic search
grep 'pattern' file.txtFind lines matching a pattern
grep -i 'pattern' file.txtCase-insensitive search
grep -r 'pattern' .Search recursively through a directory
grep -rn 'pattern' .Recursive, with file names and line numbers
grep -w 'word' file.txtMatch whole words only
Invert, count & context
grep -v 'pattern' file.txtShow lines that do NOT match
grep -c 'pattern' file.txtCount the matching lines
grep -l 'pattern' *.txtList only the files that contain a match
grep -A 3 'pattern' file.txtShow 3 lines after each match
grep -B 3 'pattern' file.txtShow 3 lines before each match
grep -C 3 'pattern' file.txtShow 3 lines around each match
Regex & matched text
grep -E 'foo|bar' file.txtExtended regex (alternation, +, ?)
grep -o 'pattern' file.txtPrint only the matched text, not the line
grep -P '\d+' file.txtPerl-compatible regex (GNU grep)
grep '^start' file.txtLines that begin with 'start'
grep 'end$' file.txtLines that end with 'end'
Pipes & file types
ps aux | grep nodeFilter another command's output
grep -rn 'TODO' --include='*.js'Search only matching file types
grep -rn 'pattern' --exclude-dir=node_modulesSkip a directory while searching

The everyday trio: -i, -r, -n

grep ("global regular expression print") searches text for lines matching a pattern, and three flags cover most real use. -i makes the search case-insensitive, -r searches recursively through every file in a directory, and -n prints the line number of each match. Combined as grep -rni 'pattern' . you get a fast, forgiving search across a whole project — the command most developers reach for dozens of times a day.

Narrowing and inverting results

When a search returns too much, a few flags refine it. -w matches whole words only (so "cat" won't match "category"), -v inverts the match to show lines that don't contain the pattern, and -c just counts matches instead of printing them. The context flags -A, -B and -C (after, before, and both) show surrounding lines, which is invaluable when a single matching line isn't enough to understand what's going on.

Regex and piping

grep's real power is regular expressions. Anchors like ^ (start of line) and $ (end of line) pin matches to position, -E enables extended regex for things like foo|bar alternation, and -o prints just the matched portion rather than the whole line — handy for extracting values. grep also shines at the end of a pipe: ps aux | grep node filters another command's output to just the lines you care about. For the full pattern syntax, see the regex reference, and pair grep with sed to edit the lines you find.

FAQ

How do I search recursively with grep?
Use grep -r 'pattern' . to search every file under the current directory, or grep -rn to add file names and line numbers. Add --include='*.js' to limit it to certain file types.
How do I make grep case-insensitive?
Add the -i flag: grep -i 'pattern' file.txt matches regardless of upper or lower case. Combine it as grep -rni 'pattern' . for a recursive, numbered, case-insensitive search.

More cheatsheets