ripgrep has support for this, regardless of the globbing syntax that is support by your shell because its support is independent of the shell.
ripgrep's -g/--glob
flag allows you to include or exclude files. And in particular, it follows the same semantics that gitignore uses (see man gitignore
). This means that if you start a glob pattern with a /
, then it will only match that specific path relative to where ripgrep is running. For example:
$ tree
.
├── a
│ └── index1.html
├── index1.html
├── index2.html
└── index3.html
1 directory, 4 files
$ rg --files
index1.html
a/index1.html
index2.html
index3.html
$ rg --files -g '!index1.html'
index2.html
index3.html
$ rg --files -g '!/index1.html'
index2.html
index3.html
a/index1.html
When using the -g
flag, the !
means to ignore files matching that pattern. When we run rg --files -g '!index1.html'
, it will result in ignoring all files named index1.html
. But if we use !/index1.html
, then it will only ignore the top-level index1.html
. Similarly, if we use !/a/index1.html
, then it would only ignore that file:
$ rg --files -g '!/a/index1.html'
index1.html
index2.html
index3.html
ripgrep has more details about the -g/--glob
flag in its guide.
!(index1.html)
this will allow all files, including any files with this filename nested in subdirectories, excluding this filename only in the current directory. – Chopping