3

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)

?

4
  • find / -type f -name '*metadata*.xml' but, i am sure that has a dozen of duplicate questions and answers. Search it. Commented Feb 15, 2017 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) Commented Feb 15, 2017 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) . Commented Feb 15, 2017 at 1:18
  • @GeorgeVasiliou doh! yes I meant -path Commented Feb 15, 2017 at 1:23

3 Answers 3

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

or, shorter (since * matches past the slashes in pathnames when used with -path),

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

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)

2
  • Works perfectly!
    – akirekadu
    Commented Feb 15, 2017 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
    Commented Feb 15, 2017 at 21:59
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 {} +
-1

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .