在 Ubuntu 终端中,我希望grep所有带有(或不包括)扩展名的文件.foo和.bar短语'foobar'.
我已经阅读了有关使用逻辑 or创建 a 的建议GLOB,并尝试了一些组合,但似乎都不起作用:
rgrep "foobar" --include "*.foo|*.bar"
rgrep "foobar" --include "*.{foo,bar}"
rgrep "foobar" --exclude "(*.foo|*.bar)"
Run Code Online (Sandbox Code Playgroud)
秘方是什么?
您可以extglob在此处使用模式:
shopt -s extglob
grep 'foobar' *.@(foo|bar)
Run Code Online (Sandbox Code Playgroud)
要使用递归 grep,--include您必须仅使用GLOB模式:
grep -R 'foobar' --include=*.{foo,bar} .
Run Code Online (Sandbox Code Playgroud)