LondonRob
LondonRob

Reputation: 78863

Logical OR in GLOB during grep --include/--exclude

In the Ubuntu terminal, I would like to grep all files with (or excluding) extension .foo and .bar for the phrase 'foobar'.

I've read this advice for creating a GLOB with a logical or, and tried a few combinations but none seem to work:

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

What's the secret recipe?

Upvotes: 4

Views: 2065

Answers (1)

anubhava
anubhava

Reputation: 785541

You can use extglob pattern here:

shopt -s extglob
grep 'foobar' *.@(foo|bar)

For using recursive grep with --include you have to use GLOB patterns only:

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

Upvotes: 5

Related Questions