ripgrep path pattern

Use the -g/--glob flag, as documented in the guide. It uses globbing instead of regexes, but accomplishes the same thing in practice. For example:

rg PM_RESUME -g '*.h'

finds occurrences of PM_RESUME only in C header files in my checkout of the Linux kernel.

ripgrep provides no way to use a regex to match file paths. Instead, you should use xargs if you absolutely need to use a regex:

rg --files -0 | rg '.*\.h$' --null-data | xargs -0 rg PM_RESUME

Breaking it down:

  • rg --files -0 prints all of the files it would search, on stdout, delimited by NUL.
  • rg '.*\.h$' --null-data only matches lines from the file list that end with .h. --null-data ensures that we retain our NUL bytes.
  • xargs -0 rg PM_RESUME splits the arguments that are NUL delimited, and hands them to ripgrep, which precisely corresponds to the list of files matching your initial regex.

Handling NUL bytes is necessary for full correctness. If you don't have whitespace in your file paths, then the command is simpler:

rg --files | rg '.*\.h$' | xargs rg PM_RESUME

Tags:

Ripgrep