Apr 28, 2022 5 min read

Using ripgrep (rg) Command in Linux

Table of Contents

ripgrep is an excellent outcome of the RIIR (re-write it in Rust) effort going on in the open source community. It is intended to be a superior replacement for the classic grep command.

The syntax to use ripgrep is as follows:

rg <pattern> [files/directories]

With ripgrep, there is no need to mention a file name. When a file name is not provided, all files are searched. This is incredibly helpful if you do not know which file contains the pattern you search.

You may also use grep to search in all files but ripgrep does it without any additional effort.

What is ripgrep?

ripgrep is a recursive regex pattern matching tool that considers your gitignore. If you have specific files, extensions, or directories in your gitignore, ripgrep will ignore them, speeding up execution time.

A few features that make ripgrep stand out are as follows:

  • Searches for patterns recursively in directories
  • Color highlighting in the output
  • Supports a large variety of encoding formats like UTF-8, SHIFT_JIS
  • Ability to search in compressed zip files
  • Ignores hidden files by default and uses your gitignore file for faster searching

You can think of it as grep, but primarily aimed at searching for files/content of files instead of the raw byte stream that grep deals with.

Install ripgrep

While grep comes preinstalled on most Linux systems, ripgrep doesn't have that privilege.

However, it is available in the repositories of all major Linux distributions, and you can use the package manager to install it.

If you are a user of Arch Linux, you already know how to install packages :p, but still, here's the command you should use:

pacman -S ripgrep

Gentoo users can install ripgrep with the following command:

emerge sys-apps/ripgrep

If you use Fedora or Red Hat, tip your hat a bit while typing this command in your terminal:

sudo dnf install ripgrep

openSUSE users (15.1 and later) should use the following command in their terminal:

sudo zypper install ripgrep

For users using Debian Buster (v10) or later, use apt. Ubuntu Cosmic Cuttlefish (18.10) or later can also use the distribution's official repositories.

sudo apt install ripgrep

Using ripgrep command

If you are familiar with grep command usage, you'll find ripgrep works similarly. You provide it with a search string and a filename, and it will search the file and show you where the input string matched with the file contents.

For this tutorial, I have cloned the repository of the dust project, and I will be executing the commands inside the cloned repository.

Basic searches

A sample search of the word description in the Cargo.html file:

$ rg description Cargo.toml
3:description = "A more intuitive version of du"
53:extended-description = """\

As expected, the ripgrep tool searched in the file that I specified and displayed the files with matching text and the line number.

demonstration of ripgrep command

If you specify multiple files to search for (if you don't specify any files, it will search for all), ripgrep will also determine the filename whose contents matched.

demonstration of ripgrep command with files to search in

Alternatively, you can also use the '--file' option, which contains the pattern you want to match. When you regularly search for a set of patterns to match, you can store it in a file and specify it using the '--file' option.

demonstration of ripgrep command with a file containing pattern

Sometimes, it is nice to have a context of the matching line, especially when searching in a code repository. The '-C' or '--context' option helps here. This option accepts a numerical value and shows lines before and after the match.

demonstration of ripgrep command for a contextual search

There can be times when you only want to see a few lines above, including the matched line. Sometimes, you want the lines only below, including the matched one.

You can use the option '-A,' short for '--after-context', and a numerical value to show lines after each match.

demonstration of ripgrep command using '--after-context' flag

And for lines before each match, you can use the option '-B,' short for '--before-context', along with a numerical value.

demonstration of ripgrep command using '--before-context' flag

Columns

There are several options regarding columns that ripgrep provides.

You will appreciate the '--column' flag if you are a vim user. It prints the 'line:column' of the matched text in the file.

demonstration of ripgrep command using '--column' flag

Another option related to columns is '-M' or '--max-columns', which takes a numeric value for the maximum number of columns. If the columns of matched lines exceed, it will let you know that a particular line was omitted from being outputted to the terminal.

demonstration of ripgrep command using '--max-columns' flag

Misc

There are several options that you can use with ripgrep.

You can use the '-s' or '--case-sensitive' option to match text with case sensitivity.

demonstration of ripgrep command using '-s' flag

If you want to keep the case insensitive, you can use the '-i' or '--ignore-case' flag.

demonstration of ripgrep command using '--ignore-case' flag

If you have a huge codebase, you can use multiple threads for pattern matching. You can manually specify threads using the '-j' or '--threads' option; it accepts a numerical value.

$ rg -j 4 TODO

There can be occasions when you want to exclude a pattern from search results. To do that, you can use the '-v' or '--invert-match' to exclude the specified pattern.

demonstration of ripgrep command using '--invert-match' command

ripgrep can search for text in a compressed archive (if the compressed file is a text file) using the '-z' or '--search-zip' flag. This flag is usually accompanied by the '-a' flag that treats binary files as text files.

demonstration of ripgrep command searching in a 'gz' archive file

Conclusion

ripgrep is a fantastic tool that you could leverage, especially as a programmer in a UNIX-like environment.

ripgrep, even though the name might suggest otherwise, is not meant to replace grep because it behaves differently. But both tools are helpful nonetheless; it depends on your use case.

Team LHB
Team LHB indicates the effort of a single or multiple members of the core Linux Handbook team.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.