grep: --exclude-dir 不起作用

wxi*_*wxi 2 grep

我想告诉grep,在 Debian 上,不要搜索/proc或 中的文件/sys。但如果我使用:

 --exclude-dir=/proc
Run Code Online (Sandbox Code Playgroud)

或者

--exclude-dir={/proc,/sys}
Run Code Online (Sandbox Code Playgroud)

或者

--exclude-dir=/proc --exclude-dir=/sys
Run Code Online (Sandbox Code Playgroud)

然后grep仍然阅读/sys并因此崩溃。那么我怎么能告诉grep跳过/proc/sys目录呢?

Kus*_*nda 7

--exclude-dirGNUgrep手册中的文档说

--exclude-dir=GLOB

跳过名称后缀与模式匹配的任何命令行目录GLOB。递归搜索时,跳过基本名称匹配的任何子目录GLOB。忽略GLOB.

如您所见,给定的模式 ( GLOB) 将仅应用于目录的实际文件名,并且由于目录名不能包含/在其名称中,因此类似的模式/proc永远不会匹配。

因此,你将不得不使用--exclude-dir=proc--exclude-dir=sys(或--exclude-dir={proc,sys}如果你是短的时间),并在同一时间意识到,这将跳过不仅/proc/sys,而且任何其他目录与名称的任何。

另一种从根向下递归搜索完整目录树同时避免这两个目录的方法是使用grepfrom find

find / \( -type d \( -path /proc -o -path /sys \) -prune \) -o \
    -type f -exec grep 'PATTERN' {} +
Run Code Online (Sandbox Code Playgroud)

这将检测这两个特定的目录/proc/sys并停止find从下降到它们。它还可以将任何找到的常规文件一次提供grep尽可能大的批次。