1. Overview

ripgrep is a command-line tool that recursively searches directories for a specific pattern. For instance, we can use it to print only the filenames that match a given pattern. This can be useful in identifying files that contain specific keywords, function names, or configuration settings, without displaying the matching lines.

In this tutorial, we’ll discuss printing only filename-matching patterns using ripgrep.

2. Installation

First, we need to install ripgrep on our system. On Ubuntu/Debian distributions, we use apt:

$ sudo apt install ripgrep

On Fedora, we’ll install it with dnf:

$ sudo dnf install ripgrep

Finally, on Arch Linux, we can execute pacman:

$ sudo pacman -S ripgrep

2.1. Basic Usage

ripgrep follows a basic syntax:

rg [options] <pattern> [path...]

Let’s break it down:

  • [options] – represents optional flags used to modify the behavior of ripgrep
  • – specifies the regex pattern we want to search for
  • [path…] – specifies the path to directories or files to search in

Now, let’s go ahead and explore how to print filenames matching a specific pattern.

3. Printing Only Filenames With Matching Patterns

To print only filenames of files that match a specific pattern, we use the -l or –files-with-matches option. This option allows us to output only the names of the files where we match the pattern, rather than the matching lines themselves.

To illustrate, let’s search for all files that contain a specific pattern and print out their names:

$ rg -l "Budget"
notes.txt
tasks.txt
...

In the example above, we search and list the names of all files that contain the string “Budget” in the current directory and its subdirectories.

Furthermore, we can search for a pattern in a specific directory by passing its path as an argument:

$ rg -l "host" /home/samuel/Desktop/Research
/home/samuel/Desktop/Research/server.conf
/home/samuel/Desktop/Research/logs/myapp.log
/home/samuel/Desktop/Research/config.json
...

Above, we search through all the files in the /home/samuel/Desktop/Research directory and list the names of the files that contain the string host.

By default, ripgrep is case-sensitive, but we can perform a case-insensitive search using the -i option:

$ rg -il "info" /home/samuel/Desktop/Research
/home/samuel/Desktop/Research/server.conf
/home/samuel/Desktop/Research/logs/application.log
/home/samuel/Desktop/Research/print/Formats.js
...

In the above command, we perform a case-insensitive search for the string “info” within all files in the /home/samuel/Desktop/Research directory and its subdirectories. Here, we’ll match patterns such as “info,” “Info,” or “INFO.”

3.2. Searching in Specific File Types

We can limit our search to certain file types using the -t option:

$ rg -lt js "import"
print/Formats.js
print/App.js
print/index.js
...

Using the above command, we search through all JavaScript files in the current directory and its subdirectories for the string “import.” We then print the names of the files that match the pattern.

Furthermore, we can search for a pattern within multiple file types by specifying them using multiple -t options:

$ rg -l -t js -t py "import"
print/Formats.js
print/App.js
print/index.js
tests/models.py
tests/urls.py
...

Above, we search for the string “import” in both JavaScript and Python files and print the filenames that match the pattern.

3.3. Excluding Files and Directories

We can exclude specific files and directories from our search using the –glob option.

To demonstrate, let’s first exclude files that match a specific pattern:

$ rg -l "import" --glob '!*.js'
tests/models.py
tests/urls.py
tests/forms.py
...

Here, we search for the string “import” in all files within the current directory and subdirectories, while excluding .js files from the search. We then print out the names of the files that match the pattern.

Similarly, we can exclude directories from our search:

$ rg -l "user" --glob '!tests/'
nginx.conf
config.json
print/settings.ini
print/settings.yaml
...

In the example above, we search for all files that contain the string “user,” while excluding files in the tests directory.

3.4. Combining Multiple Options

We can combine multiple options to perform more complex searches:

$ rg -il "error" -t log --glob '!access/*'
logs/application.log
logs/myapp.log
tests/access.log
...

In the example above, we search for the string “error” in all log files within the current directory and its subdirectories, except those in the access directory. We then print out the names of the files that match the pattern.

4. Conclusion

In this article, we looked at how to print only the file names matching a pattern using ripgrep. Furthermore, we explored how to use different options to customize our search.