Skip to content

Menu

grep and ripgrep Cheat Sheet: Flags, Regex Flavours and Ignore Rules

Checked against grep and ripgrep GNU grep 3.12 manual (Texinfo, 2 January 2025); ripgrep 16.0.0 guide; examples run with macOS grep 2.6.0-FreeBSD on 28 November 2026. Each section links to the official docs it was checked against.

This sheet puts the GNU grep options you use every day next to their ripgrep (rg) equivalents. It was checked against the GNU grep 3.12 manual, read from the Texinfo source in the release tarball because www.gnu.org was down on 28 November 2026, and against the ripgrep GUIDE for release 16.0.0. Examples ran with the BSD grep in macOS; ripgrep isn't installed here, so rg rows and GNU-only rows are marked untested.

When to use what

In a code repository, rg is usually the better default. It searches recursively without being told, skips what your .gitignore skips, ignores hidden and binary files, and is fast. That filtering is also its main surprise: if rg can't find something you know is there, the file is probably ignored. Add -u to stop honouring .gitignore, -uu to include hidden files too, and -uuu to include binaries.

Use grep when you're on a server where nothing extra is installed, in shell scripts that must run anywhere, or when you want to filter a pipe. Its exit status is reliable: 0 when a line matched, 1 when nothing did, 2 on an error, which makes grep -q the cleanest way to test for a string in a script.

On regex flavours: grep's default is basic regex, where ?, +, {}, | and () need a backslash to be special. Add -E and they work as most people expect. -P gives Perl-style regex with lookarounds in GNU grep, but the macOS grep rejected it as an invalid option. ripgrep uses its own Rust regex syntax by default and switches to PCRE2 with -P. The BRE vs ERE section below spells out which characters change meaning. Build and debug patterns in the regex tester or have one explained in the regex explainer; our guide to regex patterns every developer should know has ready-made ones.

grep -rn 'TODO' src

Search a folder recursively and show line numbers

grep -rni --exclude-dir=node_modules 'todo' .

Recursive, case-insensitive, skipping any node_modules folder

grep -rn --include='*.ts' -i todo src

Only search files whose names match the glob

grep -rl 'TODO' .

Print just the names of files that contain a match

grep -c o src/app.js

Count matching lines (not matches) in a file

printf 'a\n#b\n' | grep -v '^#'

Keep only lines that don't match, here dropping comments

printf 'x\nerr\ny\nz\n' | grep -A1 err

Show each match plus one line after it (-B before, -C both)

printf 'e1\ne2\n' | grep -m1 e

Stop after the first matching line

-R follows every symlink while recursing; -r follows only links named on the command line.

echo 'colour color' | grep -o 'colou\?r'

Basic regex (the default): ? needs a backslash to be special

echo 'colour color' | grep -oE 'colou?r'

Extended regex with -E: ?, +, | and () work unescaped

echo 'a.b axb' | grep -oF 'a.b'

Fixed string: the dot is literal, so axb no longer matches

printf 'cat\ncatalog\n' | grep -w cat

Match whole words only

printf 'a\nab\n' | grep -x a

Match only when the whole line is the pattern

grep -oP '(?<=id=)\d+' file

Perl regex with lookbehind; GNU only, macOS grep rejected -P

From the docs, not run by us
grep -z 'pattern' file

Treat input as NUL-terminated records, for use with find -print0 output

From the docs, not run by us

The special characters are . ? * + { | ( ) [ \ ^ $. In basic regex (the default, also -G), ?, +, {, |, ( and ) stay literal until you put a backslash before them; with -E they are special as typed, and \( means a literal parenthesis. -F treats each pattern as plain text, and -P switches to Perl-compatible syntax, where GNU grep's \d matches only the ASCII digits 0 to 9. Inside brackets almost everything is literal: put ] first, - last and ^ anywhere but first to match those characters themselves. Ranges like [a-d] follow ASCII order only in the C locale, so set LC_ALL=C when that matters.

echo 'aaa' | grep -o 'a\{2\}'

Interval in basic regex needs escaped braces (prints aa)

echo 'aaa' | grep -oE 'a{2}'

The same interval in extended regex, no backslashes

echo 'cat dog' | grep -o 'cat\|dog'

Alternation in basic regex is \|; macOS grep accepted it as well

printf 'abab\nabba\n' | grep -E '(ab)\1'

Back-reference: \1 must repeat what group 1 matched, so only abab prints

echo '*star' | grep -o '*s'

A * at the very start of a basic regex is an ordinary character

printf 'a1\nb\n' | grep '[[:digit:]]'

Named class inside a bracket expression; the outer brackets are required

echo 'Upper' | grep '[:upper:]'

GNU: without the outer brackets grep prints a diagnostic and exits 2

macOS grep instead read it as a set of letters and matched Upper.

From the docs, not run by us
echo 'a]b' | grep -o '[]a]'

A ] placed first in the brackets matches a literal bracket

echo 'a-b' | grep -o '[ab-]'

A - placed last is literal rather than a range

echo 'rat crate' | grep -o '\brat\b'

\b marks a word edge, so crate is skipped (\< and \> mark start and end)

echo 'b' | LC_ALL=C grep '[a-d]'

Force the C locale so a range means plain ASCII order

grep -P '\d+' file

GNU: \d covers only 0 to 9 here; [[:digit:]] also matches digits from other scripts

From the docs, not run by us

One exception to the 0/1/2 rule: with -q, grep exits 0 once a line is selected even if it also hit an error, such as an unreadable file. The manual also warns that other grep implementations may exit with a status above 2 on errors.

grep -q zzz src/app.js; echo $?

No match: exit status 1 and nothing printed

grep -q x /nonexist; echo $?

Error such as a missing file: exit status 2

if grep -q 'ready' status.txt; then ...; fi

Branch on whether a string is present without printing it

From the docs, not run by us

In ripgrep -r means --replace and only changes what is printed; it never edits files.

rg TODO

Recursive search of the current folder, respecting .gitignore

From the docs, not run by us
rg -i todo -g '*.ts'

Case-insensitive, only files matching the glob

From the docs, not run by us
rg TODO -g '!*.min.js'

Exclude a glob with a leading !

From the docs, not run by us
rg -t py import

Search only a built-in file type (-T excludes it; --type-list shows them)

From the docs, not run by us
rg -S Error

Smart case: case-insensitive unless the pattern has capitals

From the docs, not run by us
rg -F 'a.b(c)'

Search for a literal string, not a regex

From the docs, not run by us
rg --hidden TODO

Include hidden files and folders

From the docs, not run by us
rg -uu TODO

Ignore .gitignore rules and include hidden files (-uuu adds binaries)

From the docs, not run by us
rg --files

List the files rg would search, without searching

From the docs, not run by us
rg 'fn (\w+)' -r 'func $1'

Show matches with a replacement applied; files are not modified

From the docs, not run by us

Worked example: Find TODOs in your code but not in dependencies

A scratch project had a TODO in src/app.js, a lowercase one in src/lib/util.ts and one inside node_modules. Here is what three variations returned with macOS grep.

$ grep -rl 'TODO' .
./node_modules/x/index.js
./src/app.js
$ grep -rni --exclude-dir=node_modules 'todo' .
./src/app.js:1:TODO: fix
./src/lib/util.ts:1:const x = 1; // todo later
$ grep -rn --include='*.ts' -i todo src
src/lib/util.ts:1:const x = 1; // todo later

The first search is case-sensitive and recursive, so it misses the lowercase note and drags in the dependency. The second fixes both: -i catches todo, and --exclude-dir skips the vendored folder at any depth. The third narrows to TypeScript with --include. With ripgrep, rg -i todo would normally skip node_modules on its own as long as .gitignore lists it; I couldn't run rg here to show that.

Common mistakes

  • Writing grep 'a+b' and expecting + to mean one or more.

    In grep's default basic regex + is literal. Use grep -E 'a+b' or escape it as \+ in GNU grep.

  • Assuming rg searched everything when it found nothing.

    ripgrep skips gitignored, hidden and binary files. Retry with -u, -uu or --no-ignore before concluding the text isn't there.

  • Using rg -r to mean recursive, as with grep.

    In ripgrep -r is --replace. rg is already recursive, so just drop the flag.

  • Treating exit status 1 from grep as a failure in a set -e script.

    Status 1 just means no lines matched. Handle it with || true or test it in an if.

  • Searching for a version string like 1.2.3 with plain grep and getting 1x2y3 too.

    Dots match any character. Use -F for literal text, or escape the dots.