Adam Bronfin
Adam Bronfin

Reputation: 1279

How can I grep for text in specific file names under a specific directory structure recursively?

I have a directory, with thousands of other directories, each of which contains a consistent directory structure along with a specific file name I want to grep in for a pattern of text.

Let's say I have from my root DIR where I will be initiating the grep, directories like this:

/
   /dir1
      /etc
        /net
          /file.properties
   /dir2
      /etc
        /net
          /file.properties

I want search across all the directories dir1, dir2, etc, for a text pattern in all file.properties files that fall under the path "*/etc/net/file.properties"

What would my grep command look like to search all dir1, dir2...dirN for my pattern in file.properties under the path structure I specified?

What I have now is:

grep -r "text here" --include='*/net/etc/file.properties' .

Upvotes: 0

Views: 1491

Answers (2)

Eric
Eric

Reputation: 471

find . -name "file.properties" | grep "/etc/net/file.properties" | xargs grep "text here"

Upvotes: 0

arensb
arensb

Reputation: 159

The obvious answer would seem to be grep "text here" */etc/net/file.properties. Are there too many directories for this to work? If so, you might be able to do something like

for dir in *; do
    if [ -f "$dir/net/etc/file.properties" ]; then
        echo "$dir/net/etc/file.properties"
    fi
done | xargs grep "text here"

Upvotes: 1

Related Questions