0

I want to find all .xml files in all directories and subdirectories with name 'metadata' in the filesystem. Is there a way to search by pattern similar to

find . -name  */metadata/*.xml" (mixing find command and Gradle paralance)

?

  • find / -type f -name '*metadata*.xml' but, i am sure that has a dozen of duplicate questions and answers. Search it. – George Vasiliou Feb 15 '17 at 1:03
  • @GeorgeVasiliou wouldn't that need to be -path rather than -name? e.g. find / -type f -name '*/metadata/*.xml'. Also it's not clear whether the OP wants metadata anywhere in the path, or just as the immediate parent (in the latter case, find / -type f -regex '.*/metadata/[^/]*\.xml' might do it, at least in GNU find) – steeldriver Feb 15 '17 at 1:08
  • @steeldriver Yes, it might require -path , is not very clear if metadata is part of the filename (so can go at -name) or can also be part of the path , and thus -path should be used (BTW you have a typo error in your comment, right? you suggest to use -path but you wrote -name) . – George Vasiliou Feb 15 '17 at 1:18
  • @GeorgeVasiliou doh! yes I meant -path – steeldriver Feb 15 '17 at 1:23
3
$ find / -path "*/metadata/*" -name "*.xml"

or, depending on your needs, just

$ find / -path "*/metadata/*.xml"

These are not the same though.

  • The first one will find all files with an .xml suffix with a path that contains a metadata directory. The files may be several levels below that directory.

  • The second one will find all files with an .xml suffix that are located in a metadata directory.

Note: I'm using / as you said "in the filesystem", which I interpreted to mean "anywhere".

Alternatively, using locate (will only find files accessible to all users):

$ locate "/metadata/" | grep '\.xml$'

(This will work somewhat like the first find above)

  • Works perfectly! – akirekadu Feb 15 '17 at 21:56
  • I think this info is useful. I could not find anything close when I searched. Not sure why someone down voted the question :( – akirekadu Feb 15 '17 at 21:59
0

This is sort of sloppy, but while you are in the parent directory of your choosing you could try

grep -nr "*metadata/*xml" .
  • I want to search in subdirectories as well. Updated the question to make it clearer – akirekadu Feb 15 '17 at 0:19
0

What you need to realize is that the -name in 'find' is really the 'basename' Hence to get at the actual filename we can run a nested find wherein we first locate a directory whose name is 'metadata' and then look for any *.xml files under the said directory:

find . -type d -name metadata -exec sh -c '
shift $1;
while case $# in 0 ) break;; esac; do
   find "${1}/." ! -name . -prune -type f -name \*.xml
   shift
done
' 2 1 {} +

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.