user1628175 user1628175 - 1 year ago 62
Linux Question

How to exclude multiple directories that match a glob pattern in "grep -R"?

I have some directories with names like "mm-yyyy" (01-2014) in current directory. I want to grep the files in directories whose names have "2013". How can I exclude directories whose names don't have "2013"? I tried the following but it didn't work:

grep hill * -R --exclude-dir=*201[0-2]

It still went through all sub-directories to do grep.


you could use

grep -R hill *-2013

using --exclude-dir should work, too:

grep -R hill --exclude-dir=".*201[0-2]" .

without the quotes the asterisk would be expanded by bash. Additionally the wildcard for regular expressions is .*

. - matches any character
* - match any number of repetitions of the previous character, including none