The Basics
| Command | What it does |
|---|---|
| Print lines containing error | |
| Case insensitive (matches Error, ERROR) | |
| Show line numbers | |
| Invert: lines that do NOT match | |
| Count matching lines instead of printing them | |
| Match whole words only (skips passport) | |
| Match whole lines only | |
| Print only the matched text, not the whole line | |
| Stop after the first match | |
| List files that match, not the lines | |
| List files that do NOT match | |
| Fixed string: treat the pattern literally, no regex | |
| Silent; exit code only (for in scripts) |
counts matching lines, not occurrences. For total occurrences use .
Recursive Search
| Command | What it does |
|---|---|
| Search every file under the current directory | |
| Recursive with file name and line number | |
| Recursive, case insensitive | |
| Just the file names (feeds nicely into xargs) | |
| Only search .py files | |
| Skip minified files | |
| Skip a directory | |
| Skip several directories | |
| Like -r, but also follows symlinks |
Multiple Patterns
| Command | What it does |
|---|---|
| OR: match either pattern | |
| OR with extended regex | |
| Read patterns from a file, one per line | |
| AND: lines containing both | |
| Match one, exclude another | |
| Exclude several patterns | |
| Count lines matching either |
Regex
grep's default dialect is basic regex (BRE). Add for extended regex (ERE), where , , , and work without backslashes. works on every grep, including macOS.
| Pattern | Matches |
|---|---|
| Any single character | |
| Zero or more of the previous item | |
| error at the start of the line | |
| error at the end of the line | |
| Empty lines | |
| Any one of these characters | |
| Any character that is not a digit | |
| Character ranges | |
| , , | POSIX classes (portable stand-ins for \d, \s) |
| A literal dot (escape to match them literally) | |
| Word boundaries (same idea as -w) |
With :
| Pattern | Matches |
|---|---|
| One or more: gogle, google, gooogle | |
| Optional: color and colour | |
| Alternation | |
| Grouping: ab, abab | |
| Repetition counts (start of an IP address) |
unlocks Perl regex (, , lookaheads) but only exists in GNU grep. The grep on macOS and BSD does not have it: use with POSIX classes, or and call .
Context Lines
| Command | What it does |
|---|---|
| Show 3 lines After each match | |
| Show 3 lines Before each match | |
| 3 lines of Context on both sides | |
| Context plus line numbers |
Groups of matches are separated by a line. Nearby matches merge into one block.
Pipes and xargs
grep is at its best at the end of a pipe. See the awk cheat sheet for what usually comes after it.
| Command | What it does |
|---|---|
| Find a running process | |
| Same, without grep matching its own process line | |
| Search your shell history | |
| Kernel messages about USB | |
| Which proxy variables are set | |
| Which log files contain error | |
| Find files, then edit them (see the sed cheat sheet) | |
| Safe with spaces in file names |
grep on Windows
Windows has no grep built in, but both shells have an equivalent.
| Command | What it does |
|---|---|
| cmd: basic search | |
| cmd: case insensitive, recursive (roughly ) | |
| cmd: invert, like | |
| cmd: regex, in findstr's own limited dialect | |
| PowerShell: the real grep equivalent, full regex | |
| PowerShell: grep is case sensitive by default, Select-String is not | |
| PowerShell: recursive search | |
| PowerShell: sls is the short alias |
Git Bash and WSL both ship genuine GNU grep, so works there unchanged.
Gotchas
- is GNU-only. On macOS, fails with "invalid option"; use or install GNU grep (, then ).
- Always quote the pattern. Unquoted, the shell expands and against your files before grep ever sees them.
- grep exits 1 when nothing matches. In a script with that kills the script; append when no-match is fine.
- In basic regex, and are literal characters. matches the literal text go+gle; you want .
- "Binary file matches" means the file has non-text bytes. forces it to be treated as text.
- grep finds lines, sed edits them, and awk works on columns. The three cover most text jobs between them.