When searching through large code bases or directories, it’s helpful to get a sense of how many matches were found by your search query. While tools like ripgrep can give you the exact lines where a match was found, they don’t always provide an easy way to see the total number of hits. This is where ripgrep --stats and --count comes in. With these arguments we can get stats about what was found and results per file.

  • --stats to get result stats:
    • ‘matches’, ‘matched lines’, ‘files contained matches’, ‘files searched’, ‘bytes printed’, ‘bytes searched’, ‘seconds spent searching’, and ‘seconds’.
  • --count or -c to get matches per file.

--count: results per file

For example we want to search for the word “hello” and want to get the results per file. To get the results per file use --count or -c argument with ripgrep:

rg hello --count

# or simply:
rg hello -c

Output (four files found):

C:\path\file-a.txt:1               # one match in file-a.txt
C:\path\sub\file-c.txt:3           # three matches in file-c.txt
C:\anotherpath\file-b.txt:5        # five matches in file-b.txt
C:\path3\file-d.txt:1              # one match in file-d.txt

To count the number of unique files found we can use PowerShell to count the lines from the --count result:

rg hello --count | Measure-Object -Line

Output:

Lines Words Characters Property
----- ----- ---------- --------
   4

--stats: stats on results

If we use --stats we can get more detailed statistics for the rg search:

rg hello --count --stats

Output:

C:\path\file-a.txt:1
C:\path\sub\file-c.txt:3
C:\anotherpath\file-b.txt:5
C:\path3\file-d.txt:1

10 matches
10 matched lines
4 files contained matches
74 files searched
0 bytes printed
413006 bytes searched
0.001082 seconds spent searching
0.011723 seconds

If we want the first line of --stats we can use rg again to search from the results:

rg hello --count --stats | rg '^[0-9]* matches'

Output:

10 matches

If we want the three lines describing ‘matches’, ‘matched lines’, and ‘file contained matches’:

rg hello --count --stats | rg '^.*match'

Output:

10 matches
10 matched lines
4 files contained matches

If you want to see just the stats, this will show the last eight lines:

rg --stats "search" | Select-Object -last 8

Output:

10 matches
10 matched lines
4 files contained matches
74 files searched
0 bytes printed
413006 bytes searched
0.001082 seconds spent searching
0.011723 seconds

Until next time ✌️