How to Use the ‘ripgrep’ Command in Linux

Find out how ripgrep can make your Linux command-line searches faster and more precise

ripgrep, abbreviated as “rg,” is a powerful command-line search tool that stands out for its speed and efficiency. It’s designed to recursively search directories for a regex pattern, making it a go-to tool for developers, system admins, and anyone who needs to sift through large codebases or text files. Similar to commands like grep and ag (The Silver Searcher), ripgrep offers unique features like respecting your .gitignore and .ignore files, providing a more tailored search experience.

What sets ripgrep apart from its counterparts is its performance and flexibility. Whether you’re a software developer looking to find specific code snippets or a data analyst searching through large datasets, ripgrep can be an invaluable tool. It’s often used in conjunction with other commands like find and awk to create powerful search pipelines. If you’ve ever found yourself lost in a sea of text, ripgrep could be the lifebuoy that brings efficiency and precision to your workflow.

How to Install the ripgrep Command

You’ll need to install ripgrep before you can use it, as it’s not usually included by default in most Linux distributions. Here’s how you can install and uninstall it on some common distributions:

Ubuntu or Debian-based systems:

To install ripgrep, you can use the following commands:

sudo apt update
sudo apt install ripgrep

To uninstall ripgrep, you can use:

sudo apt remove ripgrep

Fedora:

To install ripgrep, use:

sudo dnf install ripgrep

To uninstall, use:

sudo dnf remove ripgrep

Arch Linux:

To install ripgrep, use:

sudo pacman -S ripgrep

To uninstall, use:

sudo pacman -R ripgrep

How to Use ripgrep

1. Search for a Specific Pattern

Syntax: rg PATTERN

Explanation: Searches for a specific pattern in the current directory.

Example: rg 'error'

Output:

src/main.c:42: printf("error: file not found");
logs/error.log:10: error: connection failed

The output shows the lines containing the word “error” in the files src/main.c and logs/error.log.

2. Search for a Pattern in a Specific File Type

Syntax: rg PATTERN -g EXTENSION

Explanation: Searches for a pattern in files with a specific extension.

Example: rg 'include' -g '*.h'

Output:

include/header.h:5: #include <stdio.h>

The output shows the line containing the word “include” in the file include/header.h.

3. Search for a Pattern and Show Line Numbers

Syntax: rg PATTERN -n

Explanation: Searches for a pattern and displays the line numbers.

Example: rg 'main' -n

Output:

src/main.c:10: int main() {

The output shows the line containing the word “main” in the file src/main.c, along with the line number 10.

4. Search for a Pattern in a Specific Directory

Syntax: rg PATTERN DIRECTORY

Explanation: Searches for a pattern in a specific directory.

Example: rg 'function' /path/to/directory

Output:

/path/to/directory/file.c:30: void my_function() {

The output shows the line containing the word “function” in the file located at /path/to/directory/file.c.

5. Search for a Pattern Case-Insensitively

Syntax: rg PATTERN -i

Explanation: Searches for a pattern without considering case.

Example: rg 'error' -i

Output:

src/main.c:42: printf("Error: file not found");
logs/error.log:10: error: connection failed

The output shows lines containing the word “error” in different cases.

6. Search for a Whole Word

Syntax: rg PATTERN -w

Explanation: Searches for a whole word matching the pattern.

Example: rg 'main' -w

Output:

src/main.c:10: int main() {

The output shows the line containing the whole word “main.”

7. Search for a Pattern and Show Context

Syntax: rg PATTERN -C NUM

Explanation: Searches for a pattern and displays NUM lines of context around each match.

Example: rg 'function' -C 2

Output:

src/file.c:28: // Start of function
src/file.c:29: {
src/file.c:30: void my_function() {
src/file.c:31: }
src/file.c:32: // End of function

The output shows the line containing “function” and two lines before and after the match.

8. Search for a Pattern and Replace with Another String

Syntax: rg PATTERN -r REPLACEMENT

Explanation: Searches for a pattern and replaces it with another string in the output.

Example: rg 'error' -r 'warning'

Output:

src/main.c:42: printf("warning: file not found");

The output shows the line with the word “error” replaced by “warning.”

9. Search for a Pattern in Files Modified Within a Specific Time

Syntax: rg PATTERN --max-filesize SIZE

Explanation: Searches for a pattern in files that are below a specific size.

Example: rg 'include' --max-filesize 1M

Output:

include/header.h:5: #include <stdio.h>

The output shows the line containing the word “include” in files that are below 1 megabyte in size.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail