June 7, 2018

ripgrep & fd - Command-line Search Tools

The grep and find utilities are ubiquitous search work-horses for Unix-like systems. The grep utility, as many reading this post will be aware, is used to search for patterns within files whilst find is primarily used to match file names based on supplied expressions.

Both of these legacy tools are still valid today many decades after their initial release. However, a new wave of tools have recently appeared that make command-line searching easier, faster and more user-friendly.

This post highlights a couple such actively-developed tools, namely ripgrep and fd.

ripgrep (pattern search)

The ripgrep utility is a line-oriented search tool that recursively searches within the current directory tree for a supplied pattern.

Written in Rust, ripgrep has excellent performance that is as fast, and more often faster, than other similar pattern search tools.

Programmer-friendliness is provided by the following niceties:

Installation

ripgrep is easily installed using Homebrew on macOS and Linuxbrew on Linux.

brew install ripgrep

For other platforms please consult these installation details.

Usage

rg Foo       # Case sensitive search
rg -i foo    # Case insensitive search
rg -v foo    # Invert search, show all lines that don't match pattern
rg -l foo    # List only the files that match, not content
rg -t md foo # Match by `md` file extension

Screenshot

ripgrep example

Ignores

As noted already, ripgrep will automatically ignore content specified in .gitignore files.

However, when not in a Git tree, or when wanting to override .gitignore, ripgrep also respects, with higher precedence, ignores specified in .rgignore files.

For instance, the next snippet will create a global ripgrep rule to ignore content in tmp directories.

echo tmp >> ~/.rgignore

Vim integration

If you are a Vim user then I recommend reading these vim-grepper details. This will nicely integrate ripgrep into Vim.

fd (file find)

The fd utility is a file finding tool. It is a modern-day simplified alternative to the Unix find tool.

Like ripgrep, fd is also written in Rust and likewise has excellent performance. Note, I am lead to believe that ripgrep and fd do share some code.

Programmer-friendliness is provided by the following niceties:

Lastly, it should be noted that fd does not provide all the functionality of find, however it sensibly provides functionality for the most common use-cases.

Installation

fd is easily installed using Homebrew on macOS and Linuxbrew on Linux.

brew install fd

For other platforms please consult these installation details.

Usage

fd              # List all non-ignored files
fd foo          # Case insensitive search
fd Foo          # Case sensitive search
fd --type f foo # Match only files
fd --type d foo # Match only directories
fd -e md foo    # Match by `md` file extension
fd '^[0-9]'     # List files starting with a digit

Demonstration

fd example

Ignores

As noted already, fd will automatically ignore content specified in .gitignore files.

However, when not in a Git tree, or when wanting to override .gitignore, fd also respects, with higher precedence, ignores specified in .fdignore files.

For instance, the next snippet will create a global fd rule to ignore content in tmp directories.

echo tmp >> ~/.fdignore

Vim CtrlP plugin

If you are a Vim user who uses the CtrlP plugin then I recommend these CtrlP with fd settings.

fzf

The fzf utility is a generic command-line fuzzy finder. If you are already using fzf and are not using fd as the file finder than I recommended adding the following settings to your shell’s configuration file (e.g ~/.bashrc).

export FZF_DEFAULT_COMMAND='fd --type f --color=never'
export FZF_ALT_C_COMMAND='fd --type d . --color=never'

If you are unfamiliar with fzf, stay tuned for an upcoming post in this channel. :eyes:

UPDATE (DEC 2018): Please read fuzzy finding in Bash with fzf and fuzzy finding in Vim with fzf

Conclusion

As an old-school command-line user I find both ripgrep and fd to now be indispensable tools. Give them a try yourself, maybe you will like them too.