Ripgrep: How to Search Only One Directory

Updated

Learn how to search only one directory with ripgrep (rg)

Ripgrep: How to Search Only One Directory

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 search only one directory, put its path right after your search pattern:

rg "pattern" path/to/directory/

Just replace pattern with what you're looking for and path/to/directory/ with your folder, for example rg "TODO" src/. ripgrep treats anything after the pattern as a path to search, and it still goes into the subfolders inside it.

If you only want the files sitting directly in that folder, add --max-depth 1 (or the short form -d 1):

rg "pattern" path/to/directory/ --max-depth 1

The results come back almost instantly, and every file path now starts with the folder you picked, so it's easy to check that nothing else was searched.

undefined

If you want to search every folder with a certain name, wherever it lives in the project, use a glob instead: rg "assert" -g '**/tests/**'.