Ripgrep 仅排除文件夹根目录中的文件

aNa*_*NaN 16 shell grep ripgrep

如果我有这样的文件夹:

\n
dir:\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 1.index1.html\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 2.index2.html\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 3.index3.html\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 a\n    \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 1.index1.html\n    \xe2\x94\x82   \n\n
Run Code Online (Sandbox Code Playgroud)\n

如何在命令中告知仅排除根文件夹中的ripgrep文件,但仍搜索in 文件夹?index1.htmlindex1.htmla

\n

Bur*_*hi5 24

ripgrep 对此支持,无论您的 shell 支持哪种通配语法,因为它的支持独立于 shell。

\n

ripgrep 的-g/--glob标志允许您包含或排除文件。特别是,它遵循 gitignore 使用的相同语义(请参阅 参考资料man gitignore)。这意味着如果您使用 a 启动 glob 模式/,那么它将仅匹配相对于 ripgrep 运行位置的特定路径。例如:

\n
$ tree\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 a\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index1.html\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index1.html\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index2.html\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index3.html\n\n1 directory, 4 files\n\n$ rg --files\nindex1.html\na/index1.html\nindex2.html\nindex3.html\n\n$ rg --files -g \'!index1.html\'\nindex2.html\nindex3.html\n\n$ rg --files -g \'!/index1.html\'\nindex2.html\nindex3.html\na/index1.html\n
Run Code Online (Sandbox Code Playgroud)\n

使用该-g标志时,!意味着忽略与该模式匹配的文件。当我们运行时rg --files -g \'!index1.html\',它将导致忽略所有名为index1.html. 但如果我们使用!/index1.html,那么它只会忽略顶层index1.html。同样,如果我们使用!/a/index1.html,那么它只会忽略该文件:

\n
$ rg --files -g \'!/a/index1.html\'\nindex1.html\nindex2.html\nindex3.html\n
Run Code Online (Sandbox Code Playgroud)\n

ripgrep-g/--glob在其指南中提供了有关该标志的更多详细信息。

\n