grep with --include和--exclude

时间:2013-01-08 17:23:30

标签: grep

我想在foo目录中搜索字符串app,但不包括文件名中包含migrations的任何文件。我希望这个grep命令能够正常工作

grep -Ir --include "*.py" --exclude "*migrations*" foo app/

上述命令似乎忽略了--exclude过滤器。作为替代方案,我可以做到

grep -Ir --include "*.py" foo app/ | grep -v migrations

这样可行,但在结果中失去了foo的突出显示。我也可以将find加入混合中并保持突出显示。

find app/ -name "*.py" -print0 | xargs -0 grep --exclude "*migrations*" foo

我只是想知道我是否遗漏了关于grep的命令行参数组合或者它们根本不能一起工作的事情。

2 个答案:

答案 0 :(得分:2)

我在.py文件上寻找一个术语,但是不想扫描迁移文件,所以我发现(对于grep 2.10)是以下(我希望这有帮助):

grep -nR --include="*.py" --exclude-dir=migrations whatever_you_are_looking_for . 

答案 1 :(得分:0)

man grep说:

       --include=GLOB
          Search only files whose base name  matches  GLOB  (using  wildcard  matching  as  described  under
          --exclude).

因为它在那里说“只”,我猜你的--include声明覆盖了你的--exclude声明。

相关问题