grep Command Cheat Sheet
A searchable reference of common grep commands and flags with what each one does.
| Command | What it does |
|---|---|
grep pattern file | Print every line in file that contains pattern. |
grep "two words" file | Search for a phrase that contains spaces by wrapping it in quotes. |
grep pattern file1 file2 | Search several files at once; each match is prefixed with its filename. |
grep pattern *.txt | Search every .txt file in the current directory using a shell glob. |
echo text | grep pattern | Filter the output of another command by piping it into grep. |
cat file | grep pattern | Pipe a file into grep, though grep pattern file is usually simpler. |
grep "exact phrase here" file | Match a multi-word phrase exactly as typed, spaces included. |
grep pattern < file | Feed a file to grep through shell input redirection. |
grep --help | Show a summary of grep options and exit. |
grep -i pattern file | Match case-insensitively, so error also matches Error and ERROR. |
grep -v pattern file | Invert the match and print only lines that do NOT contain pattern. |
grep -w pattern file | Match pattern only as a whole word, not inside a larger word. |
grep -x pattern file | Match only lines that equal pattern in full, end to end. |
grep -c pattern file | Print how many lines match instead of printing the lines. |
grep -n pattern file | Prefix each matching line with its line number. |
grep -o pattern file | Print only the matched text, not the whole line. |
grep -l pattern *.txt | List only the names of files that contain at least one match. |
grep -L pattern *.txt | List only the names of files that contain no match. |
grep -H pattern file | Print the filename before each match, even for a single file. |
grep -q pattern file | Print nothing and exit as soon as a match is found; handy in scripts. |
grep -h pattern *.txt | Suppress the filename prefix when searching multiple files. |
grep -m 5 pattern file | Stop reading a file after 5 matching lines. |
grep -s pattern file | Suppress error messages about missing or unreadable files. |
grep -a pattern file | Treat a binary file as text so matches are still printed. |
grep -r pattern . | Search every file under the current directory recursively. |
grep -R pattern . | Recurse like -r but also follow symbolic links to directories. |
grep -r --include="*.js" pattern . | Recurse but only search files whose names match the glob. |
grep -r --exclude="*.min.js" pattern . | Recurse but skip files whose names match the glob. |
grep -r --exclude-dir=node_modules pattern . | Recurse but skip entire directories by name, such as node_modules. |
grep -rn pattern . | Recurse and show a line number for every match. |
grep -rl pattern . | Recurse and list only the files that contain a match. |
grep -ri pattern . | Recurse through a tree while ignoring case. |
grep -A 3 pattern file | Print each match plus the 3 lines after it. |
grep -B 3 pattern file | Print each match plus the 3 lines before it. |
grep -C 3 pattern file | Print each match plus 3 lines of context on each side. |
grep --color=auto pattern file | Highlight the matched text in color when writing to a terminal. |
grep -n -C 2 pattern file | Combine line numbers with 2 lines of surrounding context. |
grep -E "foo|bar" file | Use extended regex, so |, +, ? and () work without backslashes. |
grep -F "a.b*c" file | Treat the pattern as a fixed string, so regex characters stay literal. |
grep -P "\d+" file | Use Perl-compatible regex for features like \d and lookaround. |
grep "^start" file | Anchor to the start of a line with ^; use pattern$ to anchor to the end. |
grep "[0-9]" file | Match any single character in a set, here any digit; [^0-9] negates it. |
grep -e foo -e bar file | Match lines containing any of several patterns given with repeated -e. |
grep -f patterns.txt file | Read the patterns to search for from a file, one pattern per line. |
grep -E "colou?r" file | Extended regex where ? makes the preceding character optional. |
grep -E "[0-9]{2,3}" file | Extended regex quantifier matching the previous item 2 to 3 times. |
grep -Eo "[a-z]+" file | Extended regex with -o to print each matched run on its own line. |
No commands match your search.
Runs entirely in your browser. Nothing you type is sent anywhere; open DevTools and watch the Network tab to verify zero requests.
What this tool does
This is a searchable quick reference for the grep commands and flags you reach for when searching text, from a plain single-file search to recursive scans across a whole project, context lines around a hit, and full regular expressions. Each row pairs the exact command with a plain-English note on what it does. Type in the filter box to search across both columns, or tap a category button to focus on one kind of task. The whole list is built into the page, so it works offline and sends nothing anywhere.
How to use it
Start typing in the Filter box. Entering recursive
surfaces the grep -r family; entering case jumps to the
case-insensitive -i flag; entering context shows the
-A, -B, and -C options. The
category buttons (Basic, Flags, Recursive, Context, Regex) narrow
the table to one group and combine with the text filter, so you can pick Regex and
type extended to zero in on grep -E. Clear the box to see
the full sheet again.
Common use cases
- Finding which files in a project mention a function name with
grep -rn name .. - Filtering a log file down to the lines that matter with
grep -i error app.log. - Counting how many times something appears using
grep -cinstead of eyeballing it. - Excluding noisy directories like build output with
--exclude-dir. - Pulling just the matched text out of a stream with
grep -oand a pattern.
Common pitfalls
- Unescaped regex metacharacters. Characters like
.,*, and?are special in a pattern. To search for them literally, quote and escape them or usegrep -Ffor a fixed-string search. - Forgetting to quote the pattern. An unquoted pattern with spaces or shell characters gets mangled by the shell before grep sees it. Wrap patterns in single or double quotes to keep them intact.
- Expecting extended syntax by default. Plain grep uses basic regex,
where
|and+are literal unless backslashed. Add-Efor extended regex or-Pfor Perl-compatible patterns. - Scanning huge trees blindly. A bare
grep -rcan crawl intonode_modulesor.gitand stall. Use--includeto target file types and--exclude-dirto skip heavy folders.
Frequently asked questions
- How do I search a whole directory recursively with grep?
- Use the -r flag and give grep a directory instead of a file, for example grep -r pattern . to search everything under the current folder. Add -n to show line numbers and -l to list only the file names that match. To narrow the search, combine -r with --include to keep certain file names or --exclude-dir to skip folders such as node_modules.
- How do I do a case-insensitive search in grep?
- Add the -i flag, so grep -i error file matches error, Error, and ERROR alike. It works alongside every other option, including recursive searches, so grep -ri error . scans a whole tree without caring about case. Drop the -i when you need an exact-case match.
- What is the difference between grep, egrep, and fgrep?
- They are the same program with different default pattern rules. Plain grep uses basic regular expressions, egrep is the same as grep -E and turns on extended regex so |, +, and parentheses work without backslashes, and fgrep is the same as grep -F and treats the pattern as a fixed string with no special characters. The standalone egrep and fgrep commands are deprecated, so prefer grep -E and grep -F in new scripts.
- How do I show the lines around a match?
- Use the context flags: -A 3 prints 3 lines after each match, -B 3 prints 3 lines before it, and -C 3 prints 3 lines on both sides. Combine them with -n to add line numbers, so grep -n -C 2 pattern file shows each hit with two surrounding lines and their positions.
- How do I search only certain file types?
- When searching recursively, add --include with a glob to restrict which file names grep opens, for example grep -r --include="*.js" pattern . limits the search to JavaScript files. Use --exclude to skip matching names or --exclude-dir to skip whole folders. Without -r you can also lean on the shell, as in grep pattern *.txt, which lets your shell expand the glob before grep runs.
- Does this grep cheat sheet send my searches anywhere?
- No. The full command list is baked into the page and every search runs locally in your browser with JavaScript, so nothing you type leaves your machine. Open your browser DevTools and watch the Network tab while you filter to confirm there are zero requests.
Cite this tool
For academic, journalistic, or technical references. Pick a format:
Citations use 2026 as the publication year. Access date is left as a fillable placeholder where the citation style expects one.