12

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?

aNaN
  • 167
  • 1
  • 1
  • 8
  • Does this answer your question? [Exclude specific filename from shell globbing](https://stackoverflow.com/questions/2643929/exclude-specific-filename-from-shell-globbing) – thanasisp Oct 15 '20 at 17:08
  • See linked post, try `!(index1.html)` this will allow all files, including any files with this filename nested in subdirectories, excluding this filename only in the current directory. – thanasisp Oct 15 '20 at 17:14
  • 1
    The linked question is not a duplicate and this question should not be closed. The linked question shows how to solve the OP's problem in a particular shell environment with a special option. But ripgrep can solve this problem independent of your shell. – BurntSushi5 Oct 16 '20 at 13:04

1 Answers1

20

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.

BurntSushi5
  • 13,917
  • 7
  • 52
  • 45