grep: --exclude-dir 名称中包含一些文本

Fra*_*ank 4 grep regular-expression

我需要使用:

grep -rio pattern --exclude-dir=".config/dir1*" --exclude-dir=".test*"
Run Code Online (Sandbox Code Playgroud)

为了避免grep名称中包含和pattern的所有文件夹,.config/dir1.test

例如.config/dir1.config/dir1/dir2、 、.test./other/.test应排除在外。

我所做的并没有排除这些文件夹,你能帮我吗?

谢谢

Gil*_*il' 7

您的命令确实.test/排除和 中的文件other/.test/

它不排除其中的文件,.config/dir1因为--exclude-dir只查看目录的基本名称,而不查看其完整路径。您需要--exclude-dir="dir1",它elsewhere/dir1也排除。请注意,如果排除某个目录,则其子目录也会被排除,因此--exclude-dir="dir1"也会排除.config/dir1/dir2.

如果 grep 的排除模式对您来说不够,您可以find与结合使用grep。的语法find更复杂,但功能更强大。例如,您可以排除.config/dir1但不排除其他名为 的目录dir1。(我假设你有 GNU find,因为你有 GNU grep。)

find . \
     -path "./.config/dir1" -prune -o \
     -name ".test" -prune -o \
     -type f -print0 |
  xargs -0 grep -io pattern
Run Code Online (Sandbox Code Playgroud)