122

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.

3
  • 1
    You could try adding a * wildcard at the beginning. find -path *content/docs/file.xml worked for me.
    – Bob
    Mar 13, 2012 at 8:58
  • thanks, @Bob, it's really worked for me. Btw, interesting, that if I'm adding a slash after an asterisk: "find -path */content/docs/file.xml", it doesn't work. Thanks a lot anyway.
    – user69817
    Mar 13, 2012 at 9:06
  • The reason the slash after asterisk didn't work is probably because the asterisk should have been escaped, see the edit to my answer.
    – Bob
    Mar 13, 2012 at 10:55

5 Answers 5

139

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

1
  • example of how to find all Tomcat's folders on the server, then list full paths for all the files contained in the WEB-INF/lib folder of each web-app: find $(find / -path '*/*tomcat*/webapps/*/WEB-INF/lib') -type f
    – Kyo
    Sep 20, 2023 at 13:14
15

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
2
  • I don't know the full path, only partial, so "-wholename" won't work.
    – user69817
    Mar 13, 2012 at 9:02
  • 1
    the flag is misleading, see my example... it just lets you extend the string into a directory if you know it Mar 13, 2012 at 9:38
4
find . -type f | grep "content/docs/file.xml"

or just

locate content/docs/file.xml
2
  • locate is an interesting option, but it searches the whole disk, I'd like to have an ability to specify the base search directory or at least to specify using param, that search should be made from this directory (like your first option does), not from the root.
    – user69817
    Mar 13, 2012 at 8:56
  • 2
    @user69817 - on the contrary, locate doesn't search the disk, it interrogates a locate database, which typically gets populated via cron once a day.
    – tink
    Aug 9, 2018 at 21:12
2

If you dont want to stay posix-compliant, at least on Linux you can also use the -regex (and -regextype) option for this purpose.

For instance:

find folder/ -regextype posix-extended -regex "(.*/)?deer/(.*/)?beer"

will match

folder/deer/beer
folder/deer/dir/forest/beer/
folder/forest/deer/dir/forest/beer/

etc.

See linux man for details.

0

For example searching files in a location using asterisk/wildcard (*) as: dir=“/apps/*/instance01/" you could use find ${dir} -name “*.jks”. putting all the files in an array like this:

arr=(`find ${dir} -name “*.jks"`)

if you want to get files with other extensions use ‘or’ like this:

-name "*.keystore" -o -name "*.jks" -o -name “*.p12" because -name only accepts single string so use ‘or’.

Finally put everything in array like this:

arr=(`find ${dir} -name "*.keystore" -o -name "*.jks" -o -name "*.p12"`)

if you have full paths not the partial paths its much easier to put them in arrays like this:

arr=(“/Users/ajay/Documents/keystore_and_p12files/"*.{keystore,p12,jks})

You must log in to answer this question.