Ripgrep to only exclude a file in the root of the folder

12.6k views Asked by At

If I have my folder like this:

dir:
    ├── 1.index1.html
    ├── 2.index2.html
    ├── 3.index3.html
    ├── a
    │   ├── 1.index1.html
    │   

How can I tell ripgrep in the command to exclude only the file index1.html in the root folder, but still searching the index1.html in folder a?

1

There are 1 answers

0
BurntSushi5 On BEST ANSWER

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.