aNa*_*NaN 16 shell grep ripgrep
如果我有这样的文件夹:
\ndir:\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\nRun Code Online (Sandbox Code Playgroud)\n如何在命令中告知仅排除根文件夹中的ripgrep文件,但仍搜索in 文件夹?index1.htmlindex1.htmla
Bur*_*hi5 24
ripgrep 对此支持,无论您的 shell 支持哪种通配语法,因为它的支持独立于 shell。
\nripgrep 的-g/--glob标志允许您包含或排除文件。特别是,它遵循 gitignore 使用的相同语义(请参阅 参考资料man gitignore)。这意味着如果您使用 a 启动 glob 模式/,那么它将仅匹配相对于 ripgrep 运行位置的特定路径。例如:
$ 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\nRun Code Online (Sandbox Code Playgroud)\n使用该-g标志时,!意味着忽略与该模式匹配的文件。当我们运行时rg --files -g \'!index1.html\',它将导致忽略所有名为index1.html. 但如果我们使用!/index1.html,那么它只会忽略顶层index1.html。同样,如果我们使用!/a/index1.html,那么它只会忽略该文件:
$ rg --files -g \'!/a/index1.html\'\nindex1.html\nindex2.html\nindex3.html\nRun Code Online (Sandbox Code Playgroud)\nripgrep-g/--glob在其指南中提供了有关该标志的更多详细信息。