12

I want to exclude both "*log*" and ./tags from grep.

What i do is:

grep -rI "PatternToSearch" ./path --exclude="*log*" 

or this:

grep -rI "PatternToSearch" ./path --exclude="tags"

is it possible to merge both exclude patterns in one grep?

DragonX
  • 371
  • 2
  • 6
  • 18
  • You could also construe it using `find`, with something like `find . -name ./tags -prune -o -not -name "*log*" -exec grep -HI Pattern {} +` – Mark Setchell Jun 10 '14 at 07:19

2 Answers2

13

Try below:

 grep -rI "PatternToSearch" ./path --exclude={*log*,tags}

Just use "," to separate patterns.

Seems duplicated with how do I use the grep --include option for multiple file types?

Community
  • 1
  • 1
Nancy
  • 1,183
  • 1
  • 7
  • 17
3

Have another --exclude <pattern>:

grep -rI "PatternToSearch" --exclude="*log*" --exclude="tags" .
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • thanks @anubhava it worked, but curious to know if there's any other way where i write `--exclude` only once – DragonX Jun 10 '14 at 06:54
  • 1
    Unfortunately `--exclude` doesn't allow regex or extended pattern to specify more than 1 pattern in single option. – anubhava Jun 10 '14 at 06:59
  • 2
    You can use `--exclude={*log*,tags}` to ignore more than 1 pattern with --exclude. – Nancy Jun 11 '14 at 05:45