How to Install "ripgrep" Tool in Linux

install ripgrep linuxRipgrep is a line oriented search tool which combines the usefulness of the silver searcher and the speed of GNU grep. It works by recursively searching the current directory path for a regex pattern ignoring hidden and binary files by default. It is currently supported on Windows, Mac OS and Linux with binary downloads available after every new release.

Why use ripgrep ?

It is faster than grep, awk and sed. Ignores binary and hidden files by default. Similarly to grep, it can search for specific types of files or ignore others explicitly. Natively it supports text encodings other than UTF and Unicode support. Ripgrep's speed comes from the Rust's regex engine from which it's built using finite automata, SIMD and other aggressive literal optimizations to account for its fast search. It also supports searching with memory maps with the use of intermediate buffers making it suitable for single files and large directories.

How to Install Ripgrep

The binary name for ripgrep is rg. Linux binaries are static executables while Windows binaries are built with MinGW (GNU) or with Microsoft Visual C++ (MSVC).

On  Mac OS X Homebrew you can install it either from homebrew-core, as follows:

$ brew install ripgrep

or you can install a binary compiled with rust nightly (including SIMD and all optimizations) by utilizing a custom tap:

$ brew tap burntsushi/ripgrep https://github.com/BurntSushi/ripgrep.git
$ brew install burntsushi/ripgrep/ripgrep-bin

If you're a Windows Chocolatey user, then you can install from the official repo.

$ choco install ripgrep

If you're an Arch Linux user, then you can install from the official repos.

$ pacman -S ripgrep

If you're a Gentoo user, you can install from the official repo.

$ emerge ripgrep

If you're a Fedora 24+ user, you can install  from copr.

$ dnf copr enable carlgeorge/ripgrep
$ dnf install ripgrep

If you're a RHEL/CentOS 7 user, you can install from copr.

$ yum-config-manager --add-repo=https://copr.fedorainfracloud.org/coprs/carlgeorge/ripgrep/repo/epel-7/carlgeorge-ripgrep-epel-7.repo
$ yum install ripgrep

If you're a Nix user, you can install from nixpkgs.

$ nix-env --install ripgrep #Or using the attribute name, which is also `ripgrep`

If you're a Rust programmer, it can be installed with cargo. Note that this requires you to have Rust 1.12 or newer installed.

$ cargo install ripgrep

ripgrep isn't currently in any other package repositories. I'd like to change that.

Few Examples

You can search any keyword recursively with the command "rg 'name'" as shown below:

$ rg plugins

This example skips all hidden and binary files in the search path. which can be explicitly stated using either '-u flag' or '-uu flags' as shown below.

$ rg -uu plugins  # similar to `grep -r` 
$ rg -uuu plugins  # similar to `grep -a -r`

Search and replace

Search for patterns from the given file, with one pattern per line. When this flag is used or multiple times or in combination with the '-e/--regexp' flag, then all patterns provided are searched. Empty pattern lines will match all input lines, and the newline is not counted as part of the pattern.  -r, --replace <ARG>  replaces every match with the string given when printing results.

1) Find first and last names and swap them using a regex pattern assigning them to two variables and applying the --replace flag

$ rg '([A-Z][a-z]+)\s+([A-Z][a-z]+)' --replace '$2, $1' #'([A..Z]..)' -> regexp flag

Searches for patterns in files with --files  flag which prints each file that would be searched without performing the search. l --files-with-matches flag shows the paths with at least one match. Also as for showing files without match(--files-without-match); print each file that would be searched without actually performing the search. This is useful to determine whether a particular file is being searched or not.

2) Search for files containing plugins in the documents directory following symbolic links

$ rg -LF 'plugins' /home/localhost/Documents/ # -L flag for following symbolic links

Include or exclude files/directories for searching that match the given glob with the -g or --glob flag. This always overrides any other ignore logic. Multiple glob flags may be used. Globbing rules match .gitignore globs. Precede a glob with a ! to exclude it.

$ rg plug -g 'README.*'
$ rg plug -g '!*.min.js' #excludes glob

 Conclusion

Ripgrep currently beats any other search tools found on the Linux system since in searching both single and huge directories of files, and it's better in both performance and correctness. Also, ripgrep provides Unicode support and works better than the currently available tools such as grep, awk, sed, find and will be an added advantage for any Linux user.

About Nyah Check

Founder of Buea Docker Meetup and Golang Buea (Buea Gophers Meetup). Interested in Cloud and Container technologies.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.