Photo by Andrew Tom on Unsplash

Photo by Andrew Tom on Unsplash

Accelerated and targeted search and find with Ripgrep

Rusty Finds

Article from ADMIN 78/2023
By
Ripgrep combines the best features of tools like Grep, Ack, and Silver Searcher when it comes to using search patterns in a terminal window.

If you want to search for specific strings in code or files, you can turn to a number of powerful Unix tools for the command line, such as Ack [1] and Grep [2]. Both use regular expressions for the search patterns. Grep is often used for this type of search, although Ack has a slight edge in terms of functionality. Silver Searcher [3], with a similar orientation, is an Ack fork that aims to boost the search speed. But these old-timers are not the subject of this article. In fact, I'm only mentioning them because they form the basis of Ripgrep.

Ripgrep [4] is a speedy implementation of Grep in the Rust language. The tool searches directories recursively with a pattern of regular expressions (regexes) and outputs all the matches it finds sorted by file. It is part of a collection of modernized Unix tools [5]. Ripgrep additionally adopts some of the features of Ack and Silver Searcher, such as searching a complete directory tree. However, it does not try to be a complete replacement for Grep, because it does not cover 100 percent of Grep's use cases.

One advantage of the Rust programming language is its speed, which is why many Linux tools have been rewritten in Rust in recent years. They include Ripgrep, which is specifically designed to make searching for strings in large files or directories as efficient as possible. The application's syntax is intuitive and easy to learn. The tool offers clear and consistent output that highlights the lines found and presents the results clearly, without requiring actions you would need to achieve comparable results in Grep, including searching for regular expressions, ignoring certain file types, or recursively searching directories. Ripgrep ignores symbolic links and hidden and binary files by default and helps Git users search for code by supporting .gitignore [6].

Another advantage is that Ripgrep is available on macOS and Windows, not just on Linux, so if you work on multiple platforms, you just need to learn the syntax for one search utility. Moreover, the current version of Ripgrep [7] is available in the repositories of many distributions. On macOS you can use Homebrew to install the package, whereas Chocolatey, Winget, or Scoop do the job on Windows.

As already mentioned, Ripgrep uses fairly easy to learn syntax. I will be looking at a few simple examples to get you started. The name of the Ripgrep executable is rg. In the simplest case, you would use the command

rg -i <search key>

to find a string in the current directory and its subdirectories. As with Grep, the -i option stands for ignoring case. If you explicitly want to see only matches with the upper- or lower-case spelling of a term, use the -s parameter followed by the search term with the desired spelling.

Ripgrep first displays the path and file name of the match before listing all occurrences of the search term with their line numbers. The application highlights the term in red (Figure 1). Depending on the search term entered, the output can be very long, even without path specification. In this case, it makes sense to limit the search command to the extent possible by entering a path (Figure 2):

Figure 1: You should only use the search without a path specification if you know that the output will be manageable.
$ rg -i bicycle Nextcloud3/Linux-User/
Figure 2: Narrowing down by stating the path gives you more targeted output. In addition to the path to the file, the tool displays the number of the line where the paragraph with the search term starts.

If you don't know the exact path or file name but know what type of file it is, the -g option is useful:

$ rg <Search key> -g '*.<Type>'

Conversely, you can exclude file types with the --type parameter:

$ rg -i bicycle --type not txt

If you don't need the individual matches, but only the files in which they occur, the -l option is useful:

$ rg -l <Search key>

You also can achieve alphabetical sorting in the context of -l by appending --sort path to the command (Figure 3).

Figure 3: If you want to know which files contain a search term, the -l --sort path option sorts the results alphabetically.

Assume you want to change the SSH port for security reasons. To do this, you want to find the line in the configuration file and jump to it directly in an editor. In this case, you would not just specify the path, but also the file containing the term. First, use

$ rg -i port /etc/ssh/sshd_config

to determine the line number. The port specification in the file shown in Figure 4 occurs in line 40. In editors like Nano or Vim, you can stipulate +40 in the call to jump directly to this line and change the port.

Figure 4: If you know the line number of the search term in a configuration file, you can jump to it directly with Nano or Vim.

Additionally, statistical values often play a key role. For example: How often does a term occur in how many lines? How long did the search take? How many files were browsed? This information appears at the end of the output if you append the --stats option to the search command (Figure 5).

Figure 5: Statistical values for the number of finds, the number of files searched, the time required, and more are provided by the --stats option.

If you want to have a little more context than Ripgrep gives you by default, set the -C <n> option, where <n> denotes the number of lines before and after the location you want to view. To check a certain number of lines exclusively before the find location, use -B <n>; -A <n> does the same thing for lines after the find. You can search compressed text archives with -z -a and a combination of options (Figure 6).

Figure 6: If so desired, Ripgrep will also dig into archives to find your quarry.

Conclusions

Ripgrep does not claim to replace Grep. On the one hand, it behaves differently in some cases; on the other hand, it does not cover all the functions of its role model.

The tool is aimed more at pattern searching, whereas Grep developer Ken Thompson designed his tool mainly for stream processing [8] on AT&T Unix v6. For example, you need to run GNU Grep in combination with find for recursive searching in directories; Ripgrep handles this task without external support.

Ripgrep offers many more functions than I can hope to describe in this article. The detailed documentation [9] will help you explore the full feature set. If you are interested in the differences between various search tools, it is also worth visiting Beyondgrep.com [10]: The site compares the features of Ack, Silver Searcher, Git-Grep, GNU Grep, and Ripgrep in detail.

The Author

Ferdinand Thommes lives and works as a Linux developer, freelance writer, and tour guide in Berlin.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus