Linux grep and ripgrep Cheat Sheet

Search text with grep, egrep, fgrep, and ripgrep including regex, file globs, recursion, and context.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Basic grep

Match lines and patterns in files and streams.

Search for a word

Print lines containing a pattern.

bashANYgrepsearch
bash
grep ERROR app.log
Notes

The simplest grep usage searches for matching lines.

Case-insensitive search

Match regardless of uppercase or lowercase.

bashANYgrepcase-insensitive
bash
grep -i timeout app.log
Notes

Useful when log or text casing is inconsistent.

Show line numbers

Display line numbers with matches.

bashANYgrepline-number
bash
grep -n TODO src/app.js
Notes

Line numbers make it easier to jump directly to a match.

Count matching lines

Only show the number of matches.

bashANYgrepcount
bash
grep -c ERROR app.log
Notes

Good for quick metrics and health checks in scripts.

Invert a match

Print lines that do not match.

bashANYgrepinvert
bash
grep -v '^#' .env
Notes

Commonly used to skip comment lines or blank lines after combining with another expression.

Match whole words only

Avoid partial substring matches.

bashANYgrepword-match
bash
grep -w user auth.log
Notes

`-w` prevents matching terms that only appear inside larger strings.

Regex with grep

Use regular expressions for more powerful matches.

Use extended regex

Enable `+`, `?`, and alternation without heavy escaping.

bashANYgrepregexextended
bash
grep -E 'warn|error|fatal' app.log
Notes

Equivalent to traditional `egrep`.

Match literal text only

Disable regex interpretation.

bashANYgrepliteralfixed-string
bash
grep -F 'a+b*c' notes.txt
Notes

Equivalent to traditional `fgrep` and useful when searching for symbols literally.

Match line start and end

Search with anchors.

bashANYgrepregexanchors
bash
grep '^ERROR.*timeout$' app.log
Notes

Anchors let you constrain a match to the full line shape.

Find blank lines

Search for empty lines.

bashANYgrepblank-lines
bash
grep -n '^$' file.txt
Notes

Helpful for validating formatting or splitting sections.

Find non-blank lines

Show lines containing at least one non-space character.

bashANYgrepwhitespace
bash
grep -n '[^[:space:]]' file.txt
Notes

Useful when skipping whitespace-only lines.

Recursive Search

Search across directories and source trees.

Search recursively

Search all files under a directory.

bashANYgreprecursive
bash
grep -R 'TODO' src/
Notes

Recursive search is a common way to scan a codebase.

Recursive search with line numbers

Show file names and line numbers for matches.

bashANYgreprecursiveline-number
bash
grep -Rn 'TODO' src/
Notes

One of the most useful codebase-search commands.

Only search matching file names

Restrict recursive search to certain extensions.

bashANYgrepincluderecursive
bash
grep -R --include='*.ts' 'useEffect' src/
Notes

Helpful for codebases containing mixed file types.

Exclude directories

Skip noisy folders like node_modules or dist.

bashANYgrepexcluderecursive
bash
grep -R --exclude-dir=node_modules 'axios' .
Notes

Excluding generated or vendored folders makes searches much faster.

List matching file names only

Show only files that contain a match.

bashANYgrepfiles
bash
grep -Rl 'TODO' src/
Notes

Useful when you only care which files match, not the lines themselves.

Context Around Matches

Show surrounding lines to understand matches better.

Show lines before a match

Print 3 lines before each match.

bashANYgrepcontext
bash
grep -B 3 ERROR app.log
Notes

Before-context is useful when logs explain the lead-up to an error.

Show lines after a match

Print 5 lines after each match.

bashANYgrepcontext
bash
grep -A 5 ERROR app.log
Notes

After-context often shows stack traces or follow-up log lines.

Show lines before and after

Print context on both sides of a match.

bashANYgrepcontext
bash
grep -C 2 ERROR app.log
Notes

`-C` gives balanced context and is often the most convenient option.

ripgrep (rg)

Fast recursive search for code and text.

Search recursively with ripgrep

Fast codebase search.

bashANYrgripgrepsearch
bash
rg TODO src/
Notes

ripgrep respects common ignore files and is usually faster than recursive grep for code.

Search with line numbers

Show matches with file and line context.

bashANYrgline-number
bash
rg -n 'useState' src/
Notes

Line numbers are included by default in many rg outputs, but `-n` makes it explicit.

Search only specific file types

Restrict search by glob.

bashANYrgglob
bash
rg -g '*.ts' 'useEffect' src/
Notes

Globs help narrow large code searches to relevant files.

Include hidden files

Search dotfiles and hidden paths.

bashANYrghidden
bash
rg --hidden 'apiKey' .
Notes

By default, hidden files are skipped in many ripgrep workflows.

Literal string search in ripgrep

Disable regex parsing for a search term.

bashANYrgliteral
bash
rg -F 'http://localhost:3000' .
Notes

Useful for URLs or strings containing regex metacharacters.

List files containing matches

Return only matching file names.

bashANYrgfiles
bash
rg -l 'TODO|FIXME' src/
Notes

Great when triaging or collecting files to edit.

Recommended next

No recommendations yet.