Skip to content

[Linux] Ripgrep (rg): A Super Fast File Search Tool

Last Updated on 2024-09-29 by Clay

Ripgrep (rg) is a command-line tool used for quickly searching file contents, designed as a replacement for grep, addressing grep's efficiency issues with large-scale file searches.

Ripgrep is written in Rust and supports an efficient regular expression engine and I/O operations, which is one of the reasons for its fast search performance.

In addition to natively supporting regular expressions, it also automatically recognizes rules from files like .gitignore, hidden files, and binary files by default.


Installation

Linux

Install using apt or yum:

sudo apt install ripgrep  # Ubuntu/Debian
sudo apt install ripgrep  # CentOS/Fedora


macOS

Install using Homebrew:

brew install ripgrep


Windows

You can download the Windows executable from the official release page, or use Scoop to install:

scoop install ripgrep

Basic Usage

The basic usage is the same as grep.

rg 'keyword'


You can also specify a particular file type to search:

rg -t py 'TODO'


You can even exclude specific files or directories (e.g., exclude everything under logs/):

rg 'error' -g '!logs/*'


It also supports searching for multiple keywords at once:

rg 'test|try'


Common options include:

  • -i: Perform a case-insensitive search.
  • -v: Invert the search, showing lines that do not match the keyword.
  • -l: Display only the names of files that contain matches.
  • -n: Display line numbers (default behavior).
  • --count: Display only the total number of matching lines.
  • --max-depth: Set the search depth to control the level of subdirectories searched.

For more advanced use, you can pipe the output to less:

rg -t py 'from|import' | less


You can also search with specific line numbers using -A (after context) or -B (before context):

rg 'keyword' -A 3 -B 2


This will display 2 lines before and 3 lines after the match.


References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version