28 Oct 2026 · linux, grep, ripgrep, terminal · 4 min
When is it worth switching from grep to ripgrep?
It is worth switching when you search large trees of code or text and want performance, .gitignore respect, and type filters without assembling a pipeline. In 20,000 files of 2 KB, ripgrep finished in 0.11 s against 0.18 s for grep -r, and did not list the binary file that contained the term. grep remains right for portable scripts and machines where you cannot install anything.
ripgrep is recursive by default, respects .gitignore inside a git repository, ignores binary files, and has type filters (-t). grep does the same with flags: -r, –include, -I. The difference is the default: rg ships with the behavior you assemble by hand in grep.
Equivalent commands
| What you want | grep | ripgrep |
|---|---|---|
| recursive search | grep -r “term” . | rg “term” |
| only .py files | grep -r –include=’*.py’ “term” . | rg -t py “term” |
| ignore binaries | grep -I | rg default |
| list files with the term | grep -rl “term” . | rg -l “term” |
| count per file | grep -c “term” file | rg -c “term” |
| case-insensitive | grep -i | rg -i |
| extended regex | grep -E | rg default |
Measurement on our machine
We generated 20,000 text files of about 2 KB, with the term ABC-123 in 7,764 of them, plus a 1 MB binary that also contains the term. We ran each once, after warming the page cache.
| Command | Time | Files with the term | Binary listed? |
|---|---|---|---|
| grep -r “ABC-123” . | 0.18 s | 7,765 | yes |
| rg “ABC-123” . | 0.11 s | 7,764 | no |
Both found the same 7,764 text files. grep listed the binary because, by default, it only prints “Binary file matches” unless you use -I. rg skipped the binary on its own. The 0.07 s difference does not justify switching by itself; the gain shows up in larger trees and in repetition, when rg’s parallelism and cache pay off.
Artifact: side-by-side test
The script below creates 200 files, puts the term in 42 of them, and creates a binary with the term. Then it runs grep and, if ripgrep is installed, rg. It cleans up the temp directory at the end.
#!/usr/bin/env bash
set -euo pipefail
D="${TMPDIR:-/tmp}/rg-vs-grep-$$"
mkdir -p "$D"
trap 'rm -rf "$D"' EXIT
for i in $(seq 1 200); do
printf 'linha %d: nada aqui\n' "$i" > "$D/f$i.txt"
done
for i in $(seq 1 42); do
printf 'achado ABC-123 no arquivo %d\n' "$i" >> "$D/f$i.txt"
done
printf 'ABC-123\n' > "$D/blob.bin"
echo "== grep =="
grep -rl "ABC-123" "$D" | wc -l
echo "== ripgrep (se instalado) =="
if command -v rg >/dev/null 2>&1; then
rg -l "ABC-123" "$D" | wc -l
else
echo "rg nao instalado neste ambiente"
fi
Save as rg-vs-grep.sh and run with bash rg-vs-grep.sh.
How to verify
grep should count 43 (42 text files plus the binary). If ripgrep is installed, it should count 42, because it skips the binary. If rg counts 43, it is running with filters disabled (-uuu) or the binary looks like text.
If it goes wrong
rg: command not found: install withapt install ripgrep(Debian/Ubuntu) or download the binary. The script continues with grep.- different results: by default rg ignores files listed in .gitignore and binary files; use
rg -uuuto disable all filters and compare with grep. - wrong accents: check
locale;LC_ALL=Cchanges the semantics of character classes. - grep slow on a large tree: use
--exclude-dirand--includeto reduce scope; or switch to rg.
When to choose which
| Situation | Choice |
|---|---|
| script that runs on any Linux | grep |
| daily search in a git repository | rg |
| tree with node_modules, .git, binaries | rg |
| machine with no installation | grep |
| need to replace text | neither (use sed or sd) |
If searching becomes routine, combine it with other tools: How to query JSON in the terminal with jq, How to make the Linux terminal nice and useful, and Disk full: how to find what is taking space.
FAQ
Is ripgrep always faster? On a single small file the difference is irrelevant. The gain is in large trees and repeated searches.
Can I use rg in CI scripts? Yes, but pin the version. grep is more predictable in minimal images.
Does rg replace sed? No. It searches. To replace, use sed or sd.
How do I search hidden files?
rg --hidden.
How do I disable all filters?
rg -uuu.
Sources
- ripgrep on GitHub, consulted on 28 Oct 2026.
- ripgrep GUIDE, consulted on 28 Oct 2026.
- GNU grep manual, consulted on 28 Oct 2026.