Alfergon
Alfergon

Reputation: 5919

How to grep files under a pattern path

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?

Upvotes: 7

Views: 11022

Answers (3)

devnull
devnull

Reputation: 123498

Specifying the -path option for find should work:

find foo/ -path "*/VIEW/*" -name "*.groovy" -exec grep method {} \;

Upvotes: 3

Chris Seymour
Chris Seymour

Reputation: 85785

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 {} \;   

Upvotes: 6

anubhava
anubhava

Reputation: 785146

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/

Upvotes: 1

Related Questions