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' srcSearch 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 srcOnly 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.jsCount 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 errShow each match plus one line after it (-B before, -C both)
printf 'e1\ne2\n' | grep -m1 eStop 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 catMatch whole words only
printf 'a\nab\n' | grep -x aMatch only when the whole line is the pattern
grep -oP '(?<=id=)\d+' filePerl regex with lookbehind; GNU only, macOS grep rejected -P
grep -z 'pattern' fileTreat input as NUL-terminated records, for use with find -print0 output
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.
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+' fileGNU: \d covers only 0 to 9 here; [[:digit:]] also matches digits from other scripts
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 ...; fiBranch on whether a string is present without printing it
In ripgrep -r means --replace and only changes what is printed; it never edits files.
rg TODORecursive search of the current folder, respecting .gitignore
rg -i todo -g '*.ts'Case-insensitive, only files matching the glob
rg TODO -g '!*.min.js'Exclude a glob with a leading !
rg -t py importSearch only a built-in file type (-T excludes it; --type-list shows them)
rg -S ErrorSmart case: case-insensitive unless the pattern has capitals
rg -F 'a.b(c)'Search for a literal string, not a regex
rg --hidden TODOInclude hidden files and folders
rg -uu TODOIgnore .gitignore rules and include hidden files (-uuu adds binaries)
rg --filesList the files rg would search, without searching
rg 'fn (\w+)' -r 'func $1'Show matches with a replacement applied; files are not modified
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. Usegrep -E 'a+b'or escape it as\+in GNU grep.Assuming
rgsearched everything when it found nothing.ripgrep skips gitignored, hidden and binary files. Retry with
-u,-uuor--no-ignorebefore concluding the text isn't there.Using
rg -rto mean recursive, as with grep.In ripgrep
-ris--replace. rg is already recursive, so just drop the flag.Treating exit status 1 from grep as a failure in a
set -escript.Status 1 just means no lines matched. Handle it with
|| trueor test it in anif.Searching for a version string like
1.2.3with plain grep and getting1x2y3too.Dots match any character. Use
-Ffor literal text, or escape the dots.
Related cheat sheets
Sources
- https://www.gnu.org/software/grep/manual/grep.html
- https://man7.org/linux/man-pages/man1/grep.1.html
- https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md
- https://github.com/BurntSushi/ripgrep/blob/master/CHANGELOG.md
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.