grep ERROR app.logThe simplest grep usage searches for matching lines.
Search text with grep, egrep, fgrep, and ripgrep including regex, file globs, recursion, and context.
Match lines and patterns in files and streams.
grep ERROR app.logThe simplest grep usage searches for matching lines.
grep -i timeout app.logUseful when log or text casing is inconsistent.
grep -n TODO src/app.jsLine numbers make it easier to jump directly to a match.
grep -c ERROR app.logGood for quick metrics and health checks in scripts.
grep -v '^#' .envCommonly used to skip comment lines or blank lines after combining with another expression.
grep -w user auth.log`-w` prevents matching terms that only appear inside larger strings.
Use regular expressions for more powerful matches.
Enable `+`, `?`, and alternation without heavy escaping.
grep -E 'warn|error|fatal' app.logEquivalent to traditional `egrep`.
grep -F 'a+b*c' notes.txtEquivalent to traditional `fgrep` and useful when searching for symbols literally.
grep '^ERROR.*timeout$' app.logAnchors let you constrain a match to the full line shape.
grep -n '^$' file.txtHelpful for validating formatting or splitting sections.
grep -n '[^[:space:]]' file.txtUseful when skipping whitespace-only lines.
Search across directories and source trees.
grep -R 'TODO' src/Recursive search is a common way to scan a codebase.
Show file names and line numbers for matches.
grep -Rn 'TODO' src/One of the most useful codebase-search commands.
Restrict recursive search to certain extensions.
grep -R --include='*.ts' 'useEffect' src/Helpful for codebases containing mixed file types.
grep -R --exclude-dir=node_modules 'axios' .Excluding generated or vendored folders makes searches much faster.
grep -Rl 'TODO' src/Useful when you only care which files match, not the lines themselves.
Show surrounding lines to understand matches better.
grep -B 3 ERROR app.logBefore-context is useful when logs explain the lead-up to an error.
grep -A 5 ERROR app.logAfter-context often shows stack traces or follow-up log lines.
grep -C 2 ERROR app.log`-C` gives balanced context and is often the most convenient option.
Fast recursive search for code and text.
rg TODO src/ripgrep respects common ignore files and is usually faster than recursive grep for code.
rg -n 'useState' src/Line numbers are included by default in many rg outputs, but `-n` makes it explicit.
rg -g '*.ts' 'useEffect' src/Globs help narrow large code searches to relevant files.
rg -F 'http://localhost:3000' .Useful for URLs or strings containing regex metacharacters.
rg -l 'TODO|FIXME' src/Great when triaging or collecting files to edit.