$linuxjunkies
>

rg(1)

A fast, line-oriented search tool that recursively searches directories for a regex pattern, similar to grep but with sensible defaults and better performance.

UbuntuDebianFedoraArch

Synopsis

rg [OPTIONS] PATTERN [PATH...]

Description

rg (ripgrep) is a modern replacement for grep and similar tools. It searches files for lines matching a regular expression pattern, automatically respecting .gitignore files and skipping binary files and hidden directories by default.

It uses parallel processing and regex optimizations to deliver results much faster than traditional grep, and provides intuitive defaults that work well for most use cases without extensive flag configuration.

Common options

FlagWhat it does
-i, --ignore-caseCase insensitive search
-w, --word-regexpMatch only whole words, as if surrounded by \b
-c, --countOnly show count of matching lines per file, not the lines themselves
-l, --files-with-matchesOnly show filenames containing matches, not the matching lines
-A NUM, --after-context NUMShow NUM lines of context after each match
-B NUM, --before-context NUMShow NUM lines of context before each match
-C NUM, --context NUMShow NUM lines of context before and after each match
-n, --line-numberShow line numbers (on by default)
-H, --with-filenameShow filename for each match (on by default for multiple files)
-t TYPE, --type TYPESearch only files of given type (e.g., rust, python, javascript)
-g GLOB, --glob GLOBInclude/exclude files matching glob pattern
--no-ignoreDon't respect .gitignore files

Examples

Search for the word TODO in all files under current directory

rg 'TODO' .

Case-insensitive search for 'error' in log files

rg -i 'error' /var/log

Search for whole word 'main' in the src directory

rg -w 'main' src/

Show 2 lines of context before and after panic! matches

rg -B2 -A2 'panic!' .

List only filenames containing 'deprecated' in src/

rg -l 'deprecated' src/

Search for function definitions in Rust files only

rg -t rust 'fn ' src/

Count import statements in Python files

rg -c '^import' --type python .

Search for pattern only in test files matching the glob

rg 'pattern' -g '*.test.js'

Related commands