batman
batman

Reputation: 3685

List through all files and find a specific file location

I want to list through .xml files in a folder. For that I did:

find . -name *.xml

The result will be file location, which I should do a cat and do a grep for the text test, if test is present then print the file location, else skip.

To begin with I tried:

find . -name *.xml | xargs cat * | grep test

but this prints the matching line, but not the file location. I tried -b, -l commands with grep to get the file location, but it doesn't work.

And cat only prints the file in the given location but not recursively accessing.

Upvotes: 0

Views: 59

Answers (2)

Benjamin W.
Benjamin W.

Reputation: 52506

You can use the globstar shell option to enable subdirectory globbing:

shopt -s globstar
grep -l 'test' **/*.xml

When globstar is enabled, ** matches "all files and zero or more subdirectories" (see the manual).

Upvotes: 0

redneb
redneb

Reputation: 23910

Try this:

find . -name *.xml -exec grep -l test {} +

This will execute grep -l test on all files found by find.

Upvotes: 2

Related Questions