首页 > 解决方案 > grep 如何排除子目录

问题描述

我使用 grep 查找一个字符串,我想用 排除一个目录grep -rl --exclude-dir=application/res --colour 'super',但它也在目录下显示匹配application/res/

而当我使用时,路径grep -rl --exclude-dir=application --colour 'super'下没有任何匹配项。application

使用 grep 时如何排除一个子目录。

grep 版本:

$ grep -V
grep (GNU grep) 2.20

grep 帮助:

  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.

标签: grep

解决方案


使用 find 您可以排除带有斜杠的整个路径:

find . -path ./application/res -prune -o -type f -exec grep -l super {} +

尽管更便携,但这将比grep -r. 但就我而言,GNU grep 没有提供排除路径的机制。


推荐阅读