How can I search a file by its name and partial path?
Often I have a file name and it's partial path, e.g. "content/docs/file.xml".
Is there a simple way to search for that file, without manually cutting into parts its name to provide directory name and file name separately?
It'd be great if find
worked in that way, so I could run find content/docs/file.xml
, but unfortunately it doesn't.
Solution 1:
Pass in a *
wildcard to indicate a match for anything. You also need to escape the *
s, e.g.:
find . -path \*content/docs/file.xml
or enclose the pattern in quotes, e.g.:
find . -path "*content/docs/file.xml"
As the man page describes it:
$ find . -name *.c -print
find: paths must precede expression
This happens because *.c has been expanded by the shell resulting in find actually receiving a command line like this:
find . -name bigram.c code.c frcode.c locate.c -print
That command is of course not going to work. Instead of doing things this way, you should enclose the pattern in quotes or escape the wild‐ card:
$ find . -name \*.c -print
Solution 2:
find has a -path
(or the equivalent but less portable -wholename
) option too find $top_dir -wholename *string*
find /usr -path *in/abiw*
>/usr/bin/abiword
Solution 3:
find . -type f | grep "content/docs/file.xml"
or just
locate content/docs/file.xml