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 / -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)
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 {} +
This is sort of sloppy, but while you are in the parent directory of your choosing you could try
grep -nr "*metadata/*xml" .
find / -type f -name '*metadata*.xml'
but, i am sure that has a dozen of duplicate questions and answers. Search it.-path
rather than-name
? e.g.find / -type f -name '*/metadata/*.xml'
. Also it's not clear whether the OP wantsmetadata
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)-path
should be used (BTW you have a typo error in your comment, right? you suggest to use -path but you wrote -name) .-path