LinuxCommandLibrary

ripgrep

Recursively search directories for a regex pattern

TLDR

View documentation for the original command

$ tldr rg
copy

SYNOPSIS

rg [options] PATTERN [PATH...]
Searches for PATTERN in files at PATHs. If no PATH is provided, it searches the current directory recursively.

PARAMETERS

-i, --ignore-case
    Perform a case-insensitive search.

-v, --invert-match
    Invert the match; select non-matching lines.

-w, --word-regexp
    Force the pattern to match only whole words.

-l, --files-with-matches
    Only print the names of files that contain matches.

-L, --files-without-matches
    Only print the names of files that do not contain matches.

-n, --line-number
    Show line numbers for matches.

-r, --recursive
    Search directories recursively. This is the default behavior.

-u, --unrestricted
    Reduce the level of 'smart' filtering. Use multiple -u for even less filtering (e.g., searching hidden files).

-g, --glob GLOB
    Include or exclude files based on a glob pattern. Can be used multiple times.

-t TYPE, --type TYPE
    Search only files of type TYPE (e.g., --type go). Use --type-list for available types.

-F, --fixed-strings
    Treat PATTERN as a literal string instead of a regular expression.

-P, --pcre2
    Use the PCRE2 regex engine for patterns that require features not supported by the default Rust regex engine.

DESCRIPTION

ripgrep (often shortened to rg) is a line-oriented search tool that recursively searches the current directory for a regex pattern. Built on Rust's regex engine, it is incredibly fast and designed to be a highly performant alternative to traditional tools like grep or The Silver Searcher (ag).

What sets ripgrep apart is its default behavior: it automatically ignores files and directories specified by .gitignore, .ignore, and .rgignore files, and also skips hidden files/directories and binary files by default. This "smart filtering" significantly reduces search times and noise. It handles Unicode characters correctly, supports various encodings, and can search specific file types. ripgrep is portable and runs on Linux, macOS, and Windows, making it a versatile tool for developers and system administrators. Its speed comes from a combination of parallel directory traversal, a highly optimized regex engine, and intelligent filtering.

CAVEATS

ripgrep's aggressive parallelism can consume significant CPU and memory resources when searching extremely large codebases or file systems without appropriate filtering. While highly optimized for common use cases, its "smart filtering" (ignoring .git, hidden files, etc.) might inadvertently hide desired results if the user is unaware of its defaults or doesn't use the -u/--unrestricted flags.

PERFORMANCE CHARACTERISTICS

ripgrep achieves its speed through several optimizations: it uses memory mapping for file access, employs a highly optimized regex engine (Rust's 'regex' crate), parallelizes directory traversal and file searching across multiple CPU cores, and intelligently skips binary files and files ignored by version control systems (.gitignore).

SMART FILTERING

By default, ripgrep automatically ignores files and directories listed in .gitignore, .ignore, and .rgignore files. It also skips hidden files/directories and binary files. This behavior can be controlled or disabled using the -u (unrestricted) flag, which can be specified multiple times for less and less filtering.

CONFIGURATION FILE

ripgrep can be configured via a configuration file, typically located at $HOME/.ripgreprc or $XDG_CONFIG_HOME/ripgrep/ripgreprc. This allows users to set default flags and behaviors without having to type them every time.

HISTORY

ripgrep was created by Andrew Gallant (also known as "BurntSushi" online) and first released in 2016. Written entirely in Rust, it was developed with the explicit goal of being faster than existing code search tools like grep and The Silver Searcher (ag), while providing a superior user experience with sensible defaults. Its rapid adoption within the developer community is largely due to its remarkable performance gains and intelligent handling of common project structures, quickly establishing itself as a preferred search utility across various operating systems.

SEE ALSO

grep(1), ack(1), ag(1), find(1)

Copied to clipboard