2015-06-02 23 views
3

在Ubuntu終端中,我想grep所有帶有(或不包括)擴展名.foo.bar的文件的短語'foobar'邏輯OR在GLOB期間在grep --include/- 排除

我讀過this advice創建與邏輯或GLOB,並嘗試了一些組合,但沒有一個似乎工作:

rgrep "foobar" --include "*.foo|*.bar" 
rgrep "foobar" --include "*.{foo,bar}" 
rgrep "foobar" --exclude "(*.foo|*.bar)" 

有什麼祕方?

回答

4

可以使用extglob模式在這裏:

shopt -s extglob 
grep 'foobar' *[email protected](foo|bar) 

對於使用遞歸的grep--include你必須使用GLOB只有模式:

grep -R 'foobar' --include=*.{foo,bar} . 
+1

這工作,但有可能使用'--exclude'和'--include'? (根據OP的標題;我也會更新這個問題以反映這一點) – LondonRob

+0

爲'--include'模式添加了一個答案。 – anubhava