[英]Ripgrep to only exclude a file in the root of the folder
如果我有這樣的文件夾:
dir:
├── 1.index1.html
├── 2.index2.html
├── 3.index3.html
├── a
│ ├── 1.index1.html
│
如何在命令中告訴ripgrep
僅排除根文件夾中的文件index1.html
,但仍在文件夾a
搜索index1.html
?
ripgrep 對此提供支持,無論您的 shell 支持什么通配語法,因為它的支持獨立於 shell。
ripgrep 的-g/--glob
標志允許您包含或排除文件。 特別是,它遵循 gitignore 使用的相同語義(參見man gitignore
)。 這意味着如果你用/
開始一個 glob 模式,那么它只會匹配相對於 ripgrep 運行位置的特定路徑。 例如:
$ 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
使用-g
標志時, !
意味着忽略匹配該模式的文件。 當我們運行rg --files -g '!index1.html'
,它會導致忽略所有名為index1.html
文件。 但是如果我們使用!/index1.html
,那么它只會忽略頂級index1.html
。 同樣,如果我們使用!/a/index1.html
,那么它只會忽略該文件:
$ rg --files -g '!/a/index1.html'
index1.html
index2.html
index3.html
ripgrep 在它的指南中有更多關於-g/--glob
標志的細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.