grep与--include和--exclude

grep与--include和--exclude,grep,Grep,我想在app目录中搜索字符串foo,但不包括文件名中包含migrations的任何文件。我希望这个grep命令能够工作 grep -Ir --include "*.py" --exclude "*migrations*" foo app/ 上面的命令似乎忽略了--exclude过滤器。作为替代,我可以这样做 grep -Ir --include "*.py" foo app/ | grep -v migrations 这是可行的,但会丢失结果中突出显示的foo。我还可以将find加入混音,并

我想在
app
目录中搜索字符串
foo
,但不包括文件名中包含
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命令行参数组合的某些内容,或者它们根本不能一起工作。

man grep
说:

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

因为上面写着“仅”,我猜您的
--include
语句将覆盖
--exclude
语句。

我在.py文件中查找一个术语,但不希望扫描迁移文件,所以我发现(对于grep2.10)如下(希望这有帮助):


这就是我在我的问题中所假设和陈述的。我只是好奇是否有办法将这两个选项结合使用。
grep -nR --include="*.py" --exclude-dir=migrations whatever_you_are_looking_for .