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.
5 Answers
Pass in a * wildcard to indicate a match for anything. You also need to escape the *s, e.g.:
find . -path \*content/docs/file.xmlor 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
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 find . -type f | grep "content/docs/file.xml"or just
locate content/docs/file.xml 2 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}) 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.