Skip to content

Ripgrep Cheatsheet: Advanced Search Techniques & Examples

Master ripgrep with our comprehensive cheatsheet, featuring advanced search techniques, sorting options, and unique command examples to streamline your coding and security research.

Perfect for developers and security professionals seeking efficiency and precision.


Install ripgrep

emerge --ask ripgrep

Install ripgrep-all

emerge --ask ripgrep-all

Colors to be used in ~/.bashrc (purple background with white foreground?)

rg -i --color=always --colors 'match:bg:160,32,240' --colors 'match:fg:255,255,255'

High-Performance recursive search using ripgrep with directory exclusions

rg -nF --hidden --no-messages --glob '!/proc/**' --glob '!/sys/**' --glob '!/dev/**' --glob '!/run/**' --glob '!/tmp/**' --glob '!/mnt/usb/**' --glob '*.{sh,conf,db,txt}' \ 'string_to_search_for' /

Alias: Colors to be used in ~/.bashrc (purple background with white foreground?)

Purple background with white font on matches for ripgrep, add to ~/.bashrc

alias rg="rg -i --color=always \
  --colors 'match:bg:160,32,240' \
  --colors 'match:fg:255,255,255'"

Sort by

rg --sort none      # (Default) Do not sort results. Fastest. Can be multi-threaded.
rg --sort path      # Sort by file path. Always single-threaded.
rg --sort modified  # Sort by the last modified time on a file. Always single-threaded.
rg --sort accessed  # Sort by the last accessed time on a file. Always single-threaded.
rg --sort created   # Sort by the creation time on a file. Always single-threaded.

Sort by descending order

rg --sortr none      # (Default) Do not sort results. Fastest. Can be multi-threaded.
rg --sortr path      # Sort by file path. Always single-threaded.
rg --sortr modified  # Sort by the last modified time on a file. Always single-threaded.
rg --sortr accessed  # Sort by the last accessed time on a file. Always single-threaded.
rg --sortr created   # Sort by the creation time on a file. Always single-threaded.

List all files contains and execute a rename script by sed

rg -l 'email@someemail.com' | sed "s|^|sed -i 's/domain1.com\.nu/domain2.com/g' |"

Search for all files of the protocols

rg -o '\b(https?|ftp|sftp|ftps|smb|rsync):\/\/[^\s"<>]+' | awk '!seen[$0]++'
rg -o '\b(https?|ftp|sftp|ftps|smb|rsync):\/\/[^\s"<>]+'

Search for all https urls

rg -o 'https:\/\/[^\s"<>]+'

Search for all protocol urls and avoid numbering / filenames

rg  --no-filename -N -o '\b(https?|ftp|sftp|ftps|smb|rsync):\/\/[^\s"<>]+' 

Search for match of protocol and include searching in zip files

rg --search-zip  --no-filename -N -o '\b(https?|ftp|sftp|ftps|smb|rsync):\/\/[^\s"<>]+'

Search for all urls that contain a zip file extension

rg --search-zip  --no-filename -N -o '\b(https?|ftp|sftp|ftps|smb|rsync):\/\/[^\s"<>]+zip'
rg --search-zip --no-filename -N -o '\b(https?|ftp|sftp|ftps|smb|rsync):\/\/[^\s"<>]+\.(?:zip|apk)'

Find pdf files in a folder recursive

  rg -. --files -g  '*.pdf'
  rg --hidden --files -g  '*.pdf'

Find all files recursive but avoid all pdf files

rg -. --files -g  '!*.pdf'   

List directories

rg --hidden \
–sort-files
–files
–null 2> /dev/null
| xargs -0 dirname
| uniq

Very fast history search with ripgrep

rh() { rg "$1" ~/.bash_history }

List matching files only

rg -l clap  

Files Without a Match

rg --files-without-match '\b(var|let|const)\b'

Use -v to filter out all matches so that only non-matches are left.

rg 'bar' | rg -v 'foobar'

Displays built-in available types and their corresponding globs

rg --type-list  

Only search in .sh files, -g can be used multiple times

rg -g '*.sh' clap  

Search in all files except .sh files, -g can be used multiple times

rg -g '!*.sh' clap 

The following command restricts the search for the pattern "key" to json files only.

rg key -t json

File separator

rg wuseman --field-match-separator ': Match -> '

List all files that would be searched without actually performing the search.

rg --files

Show the help message.

```bash

rg –help ```

Display the version of ripgrep.

```bash

rg –version ```

Use the PCRE2 regex engine.

```bash

rg –pcre2 ```

Use a provided pattern for search (requires pattern argument).

```bash

rg –regexp ```

Show column numbers in results.

```bash
rg --column
```

Don't show column numbers in results.

rg --no-column

Ignore case for the pattern.

rg --ignore

Show NUM lines after each match (requires NUM argument).

rg --after-context

Show NUM lines before and after each match (requires NUM argument).

rg --context

Use the best regex engine for each pattern.

rg --auto-hybrid-regex

Specify regex engine to use (requires argument).

rg --engine

Search binary files.

rg --binary

Search only text files.

rg --text

Search all files, including hidden and binary.

rg --unrestricted

Don't search binary files.

rg --no-binary

Use block buffering, which can help speed up searches in large data sets.

rg --block-buffered

Disable block buffering.

rg --no-block-buffered

Use line buffering, useful when tailing a file.

rg --line-buffered

Print the 0-based byte offset within the input file before each line of output.

rg --byte-offset

Show only the part of a line matching the pattern.

rg --only-matching

Use a preprocessor command.

rg --pre

Make the pattern case sensitive.

rg --case-sensitive

Ignore case when matching the pattern.

rg --ignore-case

Configure coloring of output.

rg --color

Print results in a format compatible with Vim's 'grepprg' option.

rg --vimgrep

Configure colors used in output (requires argument).

rg --colors

Display the line numbers with output lines.

rg --line-number

Show NUM lines before each match (requires NUM argument).

rg --before-context

Set the string to print between every context line.

rg --context-separator

Do not print a separator between context lines.

rg --no-context-separator

Only print a count of matches per file.

rg --count

Enable matching across multiple lines.

rg --multiline

Print the count of matches in each file instead of the matches themselves.

rg --count-matches

Always print the file path with the matched lines.

rg --with-filename

Include zero length files in the search.

rg --include-zero

Treat CRLF ("rn") as a line terminator, instead of just "n".

rg --crlf

Never treat CRLF ("rn") as a line terminator.

rg --no-crlf

Show debug messages.

rg --debug

Show trace messages. This is more verbose than –debug.

rg --trace

Set the size limit of the DFA. Requires argument for size (e.g., –dfa-size-limit 1G).

rg --dfa-size-limit

Ignore files larger than SIZE. Requires argument for size (e.g., –max-filesize 1G).

rg --max-filesize

Specify the text encoding that ripgrep should use on all files searched. Requires argument for encoding type.

rg --encoding

Never perform encoding detection and instead search all files as if they were ASCII.

rg --no-encoding

Set the field context separator. (Requires argument.)

rg --field-context-separator

Set the field match separator. (Requires argument.)

rg --field-match-separator

Print the names of files that have at least one match.

rg --files-with-matches

Print the names of files that have no matches.

rg --files-without-match

Treat the search pattern as a literal string instead of a regular expression.

rg --fixed-strings

Disable treating the search pattern as a literal string.

rg --no-fixed-strings

Follow symbolic links.

rg --follow

Do not follow symbolic links.

rg --no-follow

Only search files matching the given glob pattern.

rg --glob

Perform case-insensitive matching for glob patterns.

rg --glob-case-insensitive

Disable case-insensitive matching for glob patterns.

rg --no-glob-case-insensitive

Print the file name above each match.

rg --heading

Do not print the file name above each match.

rg --no-heading

Search hidden files and directories.

rg --hidden

Do not search hidden files and directories.

rg --no-hidden

Only search files matching the given glob pattern, case-insensitive.

rg --iglob

Read patterns to ignore from the given file.

rg --ignore-file

Read case-insensitive patterns to ignore from the given file.

rg --ignore-file-case-insensitive

Do not read case-insensitive patterns to ignore from the given file.

rg --no-ignore-file-case-insensitive

Invert the match: show lines that do not match the pattern.

rg --invert-match

Output search results in JSON format.

rg --json

Disable outputting search results in JSON format.

rg --no-json

Only match whole lines.

rg --line-regexp

Only match whole words.

rg --word-regexp

Set the maximum width of the output to N columns.

rg --max-columns

Preview the maximum width of the output without actually performing the search.

rg --max-columns-preview
rg --max-columns,

Disable previewing the maximum width of the output.

rg --no-max-columns-preview.

Limit the number of matches per file to N.

rg --max-count

Set the maximum directory depth for recursive search to N levels.

rg --max-depth

Use memory-mapped I/O to search files.

rg --mmap

Disable memory-mapped I/O for file searching.

rg --no-mmap.

Enable matching across multiple lines, treating the entire input as a single line.

rg --multiline-dotall

Disable matching across multiple lines.

rg --no-multiline

Disable matching across multiple lines, treating the entire input as a single line.

rg --no-multiline-dotall.

Do not read any configuration files.

rg --no-config

Do not display the file name with each matched line.

rg --no-filename

Do not respect ignore files.

rg --no-ignore

Do not respect ignore files that start with a dot.

rg --no-ignore-dot,

Do not respect ignore files that are part of a directory's .ignore file.

rg --no-ignore-files,

Search all files, including hidden and binary files.

rg --unrestricted.

Respect ignore files.

rg --no-ignore-dot

Respect ignore files that are part of a directory's .ignore file.

rg --ignore-dot

Do not respect ignore files that exclude patterns from the command-line.

rg --no-ignore-exclude

Respect ignore files that exclude patterns from the command-line.

rg --ignore-exclude

Do not respect ignore files that list files and directories to ignore.

rg --no-ignore-files

Do not respect global ignore files.

rg --no-ignore-global

Respect global ignore files.

rg --ignore-global

Do not respect ignore files that display warning messages.

rg --no-ignore-messages

Respect ignore files that display warning messages.

rg --ignore-messages

Do not respect ignore files that apply to parent directories.

rg --no-ignore-parent

Respect ignore files that apply to parent directories.

rg --ignore-parent

Do not respect ignore files in version control directories.

rg --no-ignore-vcs

Respect ignore files in version control directories.

rg --ignore-vcs

Do not print line numbers with the search results.

rg --no-line-number

Do not display warning messages.

rg --no-messages

Display warning messages.

rg --messages
rg --no-mmap

Enable memory-mapped I/O for file searching.

rg --mmap.

Disable PCRE2 Unicode support.

rg --no-pcre2-unicode

Disable Unicode support.

rg --no-unicode

Require that the search is performed in a Git repository.

rg --no-require-git

Use null bytes ('0') as line terminators.

rg --null

Only print the count of matches per file.

rg --count,

Use null bytes ('0') as input and output field separators.

rg --null-data
rg --text.

Do not cross file system boundaries.

rg --one-file-system

Pass through all data without filtering.

rg --passthru

Show additional lines of context around each match.

rg --context,

Set the separator for paths in the output.

rg --path-separator

Disable PCRE2 regex engine support.

rg --no-pcre2.

Show the PCRE2 version used by ripgrep.

rg --pcre2-version

Do not use a preprocessor command.

rg --no-pre

Use the given shell glob to determine files to ignore.

rg --pre-glob'

Search inside compressed files (e.g., zip, gzip).

rg --search-zip

Enable prettified output for JSON search results.

rg --pretty

Suppress all error messages.

rg --quiet

Set the maximum size of the regex pattern to N bytes.

rg --regex-size-limit

Search for an empty pattern (matches all lines).

rg --

Replace each match with the given replacement.

rg --replace

Disable searching inside compressed files.

rg --no-search-zip.

Enable smart case matching (case-insensitive when the pattern is lowercase).

rg --smart-case

Sort files by relevance in reverse order.

rg --sortr

Show statistics after search is complete.

rg --stats

Disable displaying search statistics.

rg --no-stats.

Disable searching text files.

rg --no-text.

Set the number of threads to use for searching.

rg --threads

Trim leading and trailing whitespace from each line.

rg --trim

Disable trimming leading and trailing whitespace from each line.

rg --no-trim.

Specify the file type(s) to search. (Requires type argument.)

rg --type

Add a new file type with associated globs.

rg --type-add

Clear all file type definitions.

rg --type-clear

Exclude the given file type(s) from the search. (Requires type argument.)

rg --type-not

Output the search results in Vim-compatible format.

rg --no-filename.

Search for files that do not match the given glob pattern.

rg --glob=!.git

! Example "Disable reading configuration files."

rg --no-config,

Color Examples

# Match with background color (105, 64, 130) in purple:

rg -i --colors 'match:bg:105,64,130'

# Match with underline style:

rg -i --colors 'match:style:underline'

# Match with bold style and background color (128, 0, 0) in red:

rg -i --colors 'match:bg:128,0,0' --colors 'match:style:bold'

# Match without bold style and background color (0, 128, 0) in green:

rg -i --colors 'match:bg:0,128,0' --colors 'match:style:nobold'

# Match with intense (bright) background color (0, 0, 255) in blue:

rg -i --colors 'match:bg:0,0,255' --colors 'match:style:intense'

# Match without intense (bright) background color (255, 255, 0) in yellow:

rg -i --colors 'match:bg:255,255,0' --colors 'match:style:nointense'

# Match with background color (255, 0, 255) in magenta and underline style:

rg -i --colors 'match:bg:255,0,255' --colors 'match:style:underline'

# Match with background color (0, 255, 255) in cyan without underline style:

rg -i --colors 'match:bg:0,255,255' --colors 'match:style:nounderline'

# Match with background color (255, 165, 0) in orange and line with foreground color (255, 255, 255) in white:

rg -i --colors 'match:bg:255,165,0' --colors 'line:fg:255,255,255'

# Match with background color (128, 0, 0) in red and line with foreground color (0, 255, 0) in green:

rg -i --colors 'match:bg:128,0,0' --colors 'line:fg:0,255,0'

# Match with background color (0, 128, 0) in green and line with foreground color (0, 0, 255) in blue:

rg -i --colors 'match:bg:0,128,0' --colors 'line:fg:0,0,255'

# Match with background color (0, 0, 128) in blue and line with foreground color (255, 0, 255) in magenta:

rg -i --colors 'match:bg:0,0,128' --colors 'line:fg:255,0,255'

# Match with background color (128, 128, 128) in gray and line with foreground color (255, 255, 0) in yellow:

rg -i --colors 'match:bg:128,128,128' --colors 'line:fg:255,255,0'

# Match with background color (

0, 128, 128) in teal and line with foreground color (255, 0, 0) in red:

rg -i --colors 'match:bg:0,128,128' --colors 'line:fg:255,0,0'

# Match with background color (128, 0, 128) in purple and line with foreground color (0, 255, 0) in green:

rg -i --colors 'match:bg:128,0,128' --colors 'line:fg:0,255,0'

# Match with background color (255, 0, 0) in red and line with foreground color (0, 0, 255) in blue:

rg -i --colors 'match:bg:255,0,0' --colors 'line:fg:0,0,255'

# Match with background color (0, 255, 0) in green and line with foreground color (255, 255, 255) in white:

rg -i --colors 'match:bg:0,255,0' --colors 'line:fg:255,255,255'

# Match with background color (0, 0, 255) in blue and line with foreground color (255, 0, 255) in magenta:

rg -i --colors 'match:bg:0,0,255' --colors 'line:fg:255,0,255'

# Match with background color (0, 255, 255) in cyan and line with foreground color (128, 0, 0) in maroon:

rg -i --colors 'match:bg:0,255,255' --colors 'line:fg:128,0,0'

# Match with background color (255, 0, 255) in magenta and line with foreground color (0, 128, 0) in dark green:

rg -i --colors 'match:bg:255,0,255' --colors 'line:fg:0,128,0'

# Match with background color (255, 255, 0) in yellow and line with foreground color (0, 0, 128) in navy:

rg -i --colors 'match:bg:255,255,0' --colors 'line:fg:0,0,128'

# Match with background color (255, 0, 0) in red and line with foreground color (128, 0, 128) in purple:

rg -i --colors 'match:bg:255,0,0' --colors 'line:fg:128,0,128'

# Match with background color (0, 255, 0) in green and line with foreground color (0, 128, 0) in olive:

rg -i --colors 'match:bg:0,255,0' --colors 'line:fg:0,128,0'

# Match with background color (0, 0, 255) in blue and line with foreground color (128, 0, 128) in purple:

rg -i --colors 'match:bg:0,0,255' --colors 'line:fg:128,0,128'

# Match with background color (128, 128, 128) in gray and line with foreground color (0, 128, 128) in teal:

rg -i --colors 'match:bg:128,128,128' --colors 'line:fg:0,128,128'

# Match with background color (0, 128, 128) in teal and line with foreground color (128, 128, 128) in gray:

rg -i --colors 'match:bg:0,128,128' --colors 'line:fg:128,128,128'

# Match with background color (128, 0, 128) in purple and line with foreground color (128, 0, 0) in red:

rg -i --colors 'match:bg:128,0,128' --colors 'line:fg:128,0,0'

# Match with background color (0, 0, 255) in blue and line with foreground color (0, 0, 128) in navy:

rg -i --colors 'match:bg:0,0,255' --colors 'line:fg:0,0,128'

# Match with background color (128, 128, 0) in olive and line with foreground color (128, 0, 0) in maroon:

rg -i --colors 'match:bg:128,128,0' --colors 'line:fg:128,0,0'

# Match with background color (128, 0, 128) in purple and line with foreground color (0, 128, 0) in green:

rg -i --colors 'match:bg:128,0,128' --colors 'line:fg:0,128,0'

# Match with background color (0, 128, 128) in teal and line with foreground color (0, 128, 128) in teal:

rg -i --colors 'match:bg:0,128,128' --colors 'line:fg:0,128,128'

# Match with background color (255, 0, 0) in red and line with foreground color (128, 128, 0) in olive:

rg -i --colors 'match:bg:255,0,0' --colors 'line:fg:128,128,0'

# Match with background color (0, 255, 0) in green and line with foreground color (128, 0, 128) in purple:

rg -i --colors 'match:bg:0,255,0' --colors 'line:fg:128,0,128'

# Match with background color (0, 0, 255) in blue and line with foreground color (128, 128, 128) in gray:

rg -i --colors 'match:bg:0,0,255' --colors 'line:fg:128,128,128'

# Match with background color (128, 128, 128) in gray and line with foreground color (0, 255, 0) in green:

rg -i --colors 'match:bg:128,128,128' --colors 'line:fg:0,255,0'

# Match with background color (0, 128, 128) in teal and line with foreground color (0, 0, 255) in blue:

rg -i --colors 'match:bg:0,128,128' --colors 'line:fg:0,0,255'

# Match with background color (255, 0, 0) in red and line with foreground color (0, 0, 128) in navy:

rg -i --colors 'match:bg:255,0,0' --colors 'line:fg:0,0,128'

# Match with background color (0, 255, 0) in green and line with foreground color (0, 128, 128) in teal:

rg -i --colors 'match:bg:0,255,0' --colors 'line:fg:0,128,128'

# Match with background color (0, 0, 255) in blue and line with foreground color (255, 0, 0) in red:

rg -i --colors 'match:bg:0,0,255' --colors 'line:fg:255,0,0'

# Match with background color (128, 128, 0) in olive and line with foreground color (128, 128, 0) in olive:

rg -i --colors 'match:bg:128,128,0' --colors 'line:fg:128,128,0'

# Match with background color (255, 0, 0) in red and line with foreground color (0, 255, 0) in green:

rg -i --colors 'match:bg:255,0,0' --colors 'line:fg:0,255,0'

# Match with background color (0, 0, 255) in blue and line with foreground color (0, 128, 128) in teal:

rg -i --colors 'match:bg:0,0,255' --colors 'line:fg:0,128,128'

# Match with background color (128, 128, 128) in gray and line with foreground color (255, 0, 0) in red:

rg -i --colors 'match:bg:128,128,128' --colors 'line:fg:255,0,0'

# Match with background color (0, 128, 128) in teal and line with foreground color (0, 255, 0) in green:

rg -i --colors 'match:bg:0,128,128' --colors 'line:fg:0,255,0'

# Match with background color (128, 0, 0) in red and line with foreground color (128, 128, 0) in olive:

rg -i --colors 'match:bg:128,0,0' --colors 'line:fg:128,128,0'

# Match with background color (0, 128, 0) in green and line with foreground color (0, 128, 128) in teal:

rg -i --colors 'match:bg:0,128,0' --colors 'line:fg:0,128,128'

# Match with background color (0, 0, 128) in blue and line with foreground color (128, 0, 128) in purple:

rg -i --colors 'match:bg:0,0,128' --colors 'line:fg:128,0,128'

# Match with background color (255, 255, 0) in yellow and line with foreground color (0, 255, 255) in cyan:

rg -i --colors 'match:bg:255,255,0' --colors 'line:fg:0,255,255'

# Match with background color (255, 0, 255) in magenta and line with foreground color (255, 0, 255) in magenta:

rg -i --colors 'match:bg:255,0,255' --colors 'line:fg:255,0,255'