ripgrep

rg is a fast recursive search tool. Compared to grep -r it searches the current directory by default, respects .gitignore, skips hidden files and binary files, and prints line numbers and filenames out of the box.

Install: apt install ripgrep, brew install ripgrep, or winget install BurntSushi.ripgrep.MSVC.

Search the current directory recursively

rg 'pattern'
rg 'pattern' /path/to/search

Case-insensitive, whole word, or literal string

rg -i 'pattern'      # ignore case
rg -w 'word'         # whole word only
rg -F 'a.b(c)'       # fixed string, no regex

Show context lines

rg -C 3 'pattern'    # 3 lines before and after
rg -B 2 -A 5 'pattern'

Filter by file type or glob

rg -t py 'pattern'               # only Python files
rg -T js 'pattern'               # everything except JavaScript
rg -g '*.md' 'pattern'           # glob include
rg -g '!node_modules' 'pattern'  # glob exclude
rg --type-list                   # all known types

List files or counts instead of lines

rg -l 'pattern'      # filenames that match
rg --files-without-match 'pattern'
rg -c 'pattern'      # match count per file
rg -o 'pattern'      # print only the matching part

Search for filenames

--files lists the files rg would search. Combine with -g to find by name:

rg --files                      # every searchable file
rg --files -g '*report*'        # filenames matching a glob
rg --files | rg -i 'report'     # or filter the list with a regex

Include what rg skips by default

rg --hidden 'pattern'     # also hidden files
rg -u 'pattern'           # ignore .gitignore rules
rg -uu 'pattern'          # ...and hidden files
rg -uuu 'pattern'         # ...and binary files

Multiple patterns, multiline, replacement preview

rg -e 'one' -e 'two'
rg -U 'start.*\n.*end'           # multiline
rg 'old' -r 'new'                # show output with replacement (does not edit files)

Search compressed files

rg -z 'pattern' file.gz

-z decompresses gzip, bzip2, xz, lzma, lz4, brotli and zstd single files on the fly. It does not open .zip or look inside a tarball’s members. See Search Inside Archives.

See also