grep vs ripgrep vs ag: Which Code Search Tool Should You Use?
Compare grep, ripgrep (rg), and the silver searcher (ag) across speed, defaults, gitignore handling, file type filters, and portability.
grep vs ripgrep vs ag: Which Code Search Tool Should You Use?
If you spend any time in a terminal searching source code, you have probably reached for grep, ripgrep (rg), or the silver searcher (ag). All three answer the same basic question — "where does this pattern appear in these files?" — but they make very different choices about defaults, speed, and ergonomics. This comparison walks through when each tool shines.
The short version
- grep — the POSIX standard. It is installed on essentially every Unix-like system, works in the tightest containers and oldest servers, and is the safest choice for portable shell scripts.
- ripgrep (rg) — a modern Rust rewrite. It recurses by default, respects
.gitignore, has rich file-type filters, and is generally the fastest of the three on large codebases. - ag (the silver searcher) — the tool that popularized the "gitignore-aware, faster grep" idea. It predates ripgrep and is still around, but development has slowed and ripgrep has largely displaced it.
Feature comparison
| Feature | grep | ripgrep (rg) | ag |
|---|---|---|---|
| Recursive by default | No (needs -r) | Yes | Yes |
Respects .gitignore | No | Yes | Yes |
| Skips binary files by default | No | Yes | Yes |
| Unicode / UTF-8 aware | Locale-dependent | Yes | Partial |
| PCRE / lookaround | With -P (GNU only) | With --pcre2 | Limited |
| File-type filters (e.g. HTML, JS) | No (use --include) | Yes (-t html, -t js) | Yes (--html) |
| Ships with the OS | Almost always | Often needs install | Needs install |
| Active development | Stable / mature | Very active | Slow |
Flag equivalence cheat sheet
| Task | grep | ripgrep | ag |
|---|---|---|---|
| Recursive search | grep -r pattern . | rg pattern | ag pattern |
| Only HTML files | grep -r --include='*.html' pattern . | rg -t html pattern | ag --html pattern |
| Case-insensitive | grep -i | rg -i | ag -i |
| Whole word | grep -w | rg -w | ag -w |
| Show line numbers | grep -n | on by default | on by default |
| Fixed string (no regex) | grep -F | rg -F | ag -Q |
| Files with matches only | grep -l | rg -l | ag -l |
| Ignore a path | --exclude-dir=node_modules | -g '!node_modules' | --ignore-dir node_modules |
| Include hidden/ignored | N/A | rg -uu or --no-ignore | ag -u |
Speed considerations
On small directories, all three tools feel instant. Differences show up on large repositories — thousands of files, deep node_modules trees, or monorepos. As a general ordering: ripgrep tends to be the fastest, ag the middle, and grep the slowest once you get past a few thousand files. Ripgrep wins on big trees for several reasons:
- It reads and searches files in parallel across CPU cores.
- Its regex engine builds efficient literal prefilters, avoiding work on non-matching lines.
- It skips ignored directories entirely rather than descending and filtering afterward.
Grep is not slow because of a bad implementation — it is slow relative to rg because it does not know to skip .git, node_modules, build artifacts, or binary blobs unless you tell it to. Point grep at only the files you care about and the gap narrows considerably. Exact numbers depend heavily on hardware, filesystem cache, and repo shape, so treat any single benchmark you read as a data point, not a verdict.
When each tool is the right choice
Reach for grep when
- You are writing a portable shell script that has to run on unfamiliar machines.
- You are inside a minimal container, a rescue shell, or an embedded system.
- You are searching a small, known set of files where install time is not worth it.
Reach for ripgrep when
- You are searching a real project with a
.gitignore— rg respects it out of the box. - You want to constrain by language:
rg -t html,rg -t js,rg -t py. - You need modern regex features, Unicode correctness, or PCRE2 lookarounds.
- Speed on a large codebase matters and you can install one binary.
Reach for ag when
- It is already installed on the box and you know its flags.
- You are supporting an existing toolchain, editor plugin, or muscle-memory workflow built around it.
For most new work, ripgrep is the pragmatic upgrade path from ag — the defaults are similar, the flags are close, and the ecosystem (editor plugins, CI) has largely moved to it.
Bottom line
Keep grep in your toolkit forever — it is universal and unkillable. Install ripgrep on any machine where you actually read code. Treat ag as the tool that showed everyone what a modern code search could feel like, even if ripgrep has since taken the crown.