1

Situation: In Linux, I have a parent folder with 22 folders of various names, each with a file of a particular name asset.xml. Also in these folders are hundreds of other folders with asset.xmlin them, but these are previous versions and I'm not interested in them. I need to search inside each file for 3 tags "legend|assetID|name" and return the information inside these tags. They're usually on the same line so i think Grep is ok.

What i've tried:

grep -nr -E "legend|assetID|name" . > /dir/to/the/ReportFile.txt

This takes way too long and return way too much duplicated data, so this isn't practical.

find . -maxdepth 2 -exec grep -E "legend|assetID|name" . > /dir/to/the/ReportFile.txt

This returned an error with -exec missing an argument, so the output file was empty.

find . -maxdepth 2 -| grep -E "legend|assetID|name" . > /dir/to/the/ReportFile.txt

This returned an empty output file.

Question: How do I go through each like-named file each inside various-named folders (which also have other subfolders with files of the same name) and extract information inside the 3 tags and write this data into an output file?

1

I just found the solution.

grep -nr -E "legend|assetID|name" /dir/to/the/*/asset.xml > /dir/to/the/ReportFile.txt

The * in the path is the "wildcard" I needed to go through each directory. I think it only goes to that directory and not into any deeper folder...?

1

Your command line with find was almost correct :-)

The file(s) found by find is represented by {}. The -exec part of the find command line must be finished somehow, with \; or often better with + which invokes grep only once, and I suggest that you run grep only on normal files -type f, try

find . -maxdepth 2 -type f -exec grep -E "legend|assetID|name" {} + > ../output

You may want to specify the file name asset.xml

find . -maxdepth 2 -type f -name asset.xml -exec grep -E "legend|assetID|name" {} + > ../output

Check the result with

cat ../output

You may want to modify the maxdepth to 3 or whatever is suitable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.