Ripgrep: How to Count Matches

Updated

Learn how to count matches per file and in total with ripgrep (rg)

Ripgrep: How to Count Matches

Before we start, make sure you have ripgrep installed on your computer. On a Mac, you can install it with Homebrew:

brew install ripgrep

First, open your terminal and navigate to your project folder. To count how many lines match in each file, run:

rg -c "pattern"

You'll get one line per file with the file name and a number. Just keep in mind that -c counts matching lines, so a line that contains your word twice only counts once. To count every single occurrence, use --count-matches instead:

rg --count-matches "pattern"

Both of these give you counts per file. For one grand total across the whole project, print each match on its own line with -o and count the lines with wc -l:

rg -o "pattern" | wc -l

This finishes almost instantly, even on big projects, and prints a single number.

undefined

If you want a full summary, run rg -q --stats "pattern". The -q flag hides the matching lines, so you only see the total matches, matched lines and files searched.