Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep files under a pattern path

Tags:

grep

unix

Let's say there is the following tree of directories:

foo/
   +bar/
   |   +VIEW/
   |   |    +other/
   |   |          +file.groovy
   |   |    +file2.groovy
   |   |    +file.txt
   |   +file3.groovy
   +xyz/
       +VIEW/
            +file4.groovy
       +file5.groovy

And I want to grep method only on groovy files that are into any directory that either is VIEW or is under VIEW (that would be file.groovy, file2.groovy and file4.groovy).

I'm trying to do so using --include=.*\/VIEW\/.*\.groovy which as far as I know is the regex for anything and then / character and then the word VIEW and then / character and then anything and then the word .groovy

But that actually doesn't return anything.

Isn't there a way of doing this using the include option?

like image 900
Alfergon Avatar asked Aug 13 '13 10:08

Alfergon


3 Answers

From the UNIX philosophy:

Write programs that do one thing and do it well. Write programs to work together.

I don't like the GNU extension for recursive directory searching. The tool find does a much better job, has a cleaner syntax with more options and doesn't break the philosophy!

$ find foo/*/VIEW -name "*.groovy" -exec grep method {} \;   
like image 153
Chris Seymour Avatar answered Sep 30 '22 08:09

Chris Seymour


Specifying the -path option for find should work:

find foo/ -path "*/VIEW/*" -name "*.groovy" -exec grep method {} \;
like image 31
devnull Avatar answered Sep 30 '22 08:09

devnull


You don't need to provide directory path in include= option, just provide the path as last argument to recursive grep.

Thus you can simply do:

grep -R 'method' --include=*.groovy foo/bar/VIEW/
like image 41
anubhava Avatar answered Sep 30 '22 09:09

anubhava