LinuxCommandLibrary

rgrep

Recursively search files for a pattern

TLDR

Recursively search for a pattern in the current working directory

$ rgrep "[search_pattern]"
copy

Recursively search for a case-insensitive pattern in the current working directory
$ rgrep [[-i|--ignore-case]] "[search_pattern]"
copy

Recursively search for an extended regex pattern (supports ?, +, {}, () and |) in the current working directory
$ rgrep [[-E|--extended-regexp]] "[search_pattern]"
copy

Recursively search for an exact string (disables regex) in the current working directory
$ rgrep [[-F|--fixed-strings]] "[exact_string]"
copy

Recursively search for a pattern in a specified directory (or file)
$ rgrep "[search_pattern]" [path/to/file_or_directory]
copy

SYNOPSIS

rgrep [OPTIONS] PATTERN [DIRECTORY...]

PARAMETERS

PATTERN
    The regular expression or fixed string to search for.

DIRECTORY...
    One or more directories to search recursively. Defaults to the current directory if not specified.

-r, --recursive
    This is the fundamental option for `rgrep`, indicating a recursive search within directories. It's typically implied or directly part of the `rgrep` alias for `grep`.

-R, --dereference-recursive
    Similar to -r, but follows symbolic links during the recursive traversal.

-i, --ignore-case
    Ignore case distinctions in both the PATTERN and input files.

-v, --invert-match
    Select non-matching lines; display lines that do not contain the PATTERN.

-l, --files-with-matches
    Suppress normal output; instead print the name of each input file from which output would normally have been printed.

-n, --line-number
    Prefix each line of output with the 1-based line number within its input file.

-w, --word-regexp
    Select only those lines containing matches that form whole words.

-E, --extended-regexp
    Interpret PATTERN as an extended regular expression (ERE).

-P, --perl-regexp
    Interpret PATTERN as a Perl-compatible regular expression (PCRE).

--exclude=GLOB
    Skip files whose basename matches GLOB (e.g., --exclude='*.log').

--include=GLOB
    Search only files whose basename matches GLOB (e.g., --include='*.txt').

--exclude-dir=GLOB
    Exclude directories whose basename matches GLOB from the recursive search (e.g., --exclude-dir='.git').

DESCRIPTION

`rgrep` is commonly a shorthand or an alias for the `grep` command invoked with the -r (or --recursive) option. It enables searching for a specified pattern within files in a given directory and all its subdirectories. Unlike a basic `grep` command which typically searches only within files explicitly listed or from standard input, `rgrep` automates the traversal of the directory tree. This makes it highly useful for codebases, log directories, or any scenario where a pattern might exist in multiple nested files without knowing their exact paths beforehand.

While `rgrep` itself is not a standalone binary on most systems, understanding its function is crucial for efficient command-line pattern searching across entire file systems or project directories. It leverages the robust features of the `grep` utility, extending its capabilities to recursive operations.

CAVEATS

The primary caveat for `rgrep` is that it is typically not a standalone executable or command on most Linux distributions. Instead, it commonly functions as an alias (e.g., alias rgrep='grep -r') or a shell function that simplifies the invocation of grep --recursive. Consequently, all options, behavior, and limitations of `rgrep` are inherited directly from the underlying `grep` utility, specifically when used in its recursive mode. Users should be aware that `rgrep` does not follow symbolic links by default; for this, the -R or --dereference-recursive option (part of `grep`) is required. Recursive searches on very large directory structures or network file systems can also be resource-intensive and slow.

USAGE EXAMPLES

Here are some common ways `rgrep` (or `grep -r`) can be used:

Search for 'hello' in all files in the current directory and subdirectories:
rgrep 'hello' .
or
grep -r 'hello' .

Search for 'ERROR' case-insensitively in log files, excluding 'old_logs' directory:
rgrep -i 'ERROR' --include='*.log' --exclude-dir='old_logs' /var/log

Find files containing 'function_name' and display line numbers, only in C source files:
rgrep -n 'function_name' --include='*.c' ~/my_project

PERFORMANCE AND ALTERNATIVES

`rgrep` (i.e., `grep -r`) is very capable but can be slow on extremely large codebases or network drives. For faster recursive searches, especially in development environments, consider modern alternatives like rg (ripgrep) or ack. These tools are often optimized for speed and user experience in recursive pattern matching.

HISTORY

The concept of `rgrep` is closely tied to the evolution of the `grep` utility. `grep` itself was developed by Ken Thompson in 1970. The capability for recursive searching, typically invoked via the -r or --recursive option, was added to `grep` as file systems grew larger and hierarchical directory structures became more common. This enhancement allowed users to search across entire directory trees efficiently. While `grep` has maintained its core functionality, features specific to recursive searching (like options to include/exclude files and directories) have been introduced over time to address more complex use cases. `rgrep` as a distinct command or common alias emerged as a user convenience, providing a simpler and quicker way to access `grep`'s powerful recursive search feature without always explicitly typing the -r flag.

SEE ALSO

grep(1), find(1), xargs(1), ack(1), rg(1)

Copied to clipboard