用grep排除目录

时间:2019-09-05 22:48:47

标签: linux ubuntu grep

我刚刚尝试过:

michael@Pascal:~/noisynet$ sudo grep -rio --exclude-dir={/ece,/home/michael/pytorch,/sys,/proc} 'hello' /

第一场比赛是:

/home/michael/pytorch/.git/logs/HEAD:hello

为什么在/home/michael/pytorch中显示?

1 个答案:

答案 0 :(得分:1)

这会很好

grep -rio --exclude-dir={ece,pytorch,sys,proc} 'hello' /

注意:这还将排除具有相同名称的其他目录。

说明:

grep的手册页给出了以下代码段

   --exclude-dir=GLOB
          Skip  any command-line directory with a name suffix that matches the pattern GLOB.  When
          searching recursively, skip any subdirectory whose base name matches GLOB.   Ignore  any
          redundant trailing slashes in GLOB.

这意味着给定的模式(GLOB)将仅应用于目录的实际名称,并且由于目录名中不包含/,因此/ proc之类的模式将永远不匹配。

因此,我们只需要使用--exclude-dir=proc or --exclude-dir=sys (or --exclude-dir={proc,sys})来排除不带'/'的目录名称。