user1628175
user1628175

Reputation: 303

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.

Upvotes: 26

Views: 26318

Answers (2)

Max Fichtelmann
Max Fichtelmann

Reputation: 3504

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

Upvotes: 20

Rebekah Waterbury
Rebekah Waterbury

Reputation: 23796

grep -r --exclude-dir={dir1,dir2,dir3} string_to_search_for *

Upvotes: 57

Related Questions