Ripgrep to only exclude a file in the root of the folder
Asked Answered
T

1

18

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?

Theurgy answered 15/10, 2020 at 13:54 Comment(3)
Does this answer your question? Exclude specific filename from shell globbingChopping
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.Chopping
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.Pushkin
P
26

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.

Pushkin answered 16/10, 2020 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.