Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

grep

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.

like image 618
user1628175 Avatar asked Sep 10 '25 16:09

user1628175


2 Answers

grep -r --exclude-dir={dir1,dir2,dir3} string_to_search_for *
like image 169
Rebekah Waterbury Avatar answered Sep 13 '25 15:09

Rebekah Waterbury


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
like image 32
Max Fichtelmann Avatar answered Sep 13 '25 14:09

Max Fichtelmann