Showing results for 
Search instead for 
Did you mean: 

find command with wildcards

Go to solution

find command with wildcards

hy admin's
i'll try to write my first shell script. I search for files in a directorytree which ended with htm, html, doc or pdf. The command ls /path *.@(htm|html|doc|pdf) shows the right files, but in a wrong output. Therefor i want to use the command find. With the commandline :
find /path -type f -iname '*.htm' -printf '\n %P' i get the right output, but the wildcards like in the ls-command don't work.
Where is my problem!
Werner Placke
Duncan Edmonstone
Honored Contributor

Re: find command with wildcards

Try using double quotes



Honored Contributor

Re: find command with wildcards

You will have to use with find -o (OR):
find / \( -name \*.htm -o -name \*.html etc...

All the best

Praveen Bezawada
Respected Contributor

Re: find command with wildcards


ls /path/*.@(html|html|pdf) is working for me.
I think you may have missed the second /

Roger Baptiste
Honored Contributor

Re: find command with wildcards

This should work :
find /path \( -name "*.htm" -o -name "*.html" -o -name "*.doc" -name "*.pdf" \) -print

This should also work:

find /path |grep -e "*.html" -e "*.doc" -e "*.pdf" -e "*.htm"

Take it easy.
David Lodge
Trusted Contributor

Re: find command with wildcards

This is a problem with the way the shell intreprets things.

In the shell the wildcard pattern (called a glob) is parsed by the shell *before* it is passed to the command, you can see this by typing set -x before running a command:
ls !(lost+found)
print !(lost+found)

In the case of you find line - as soon as you quote a CLI parameter, whether using single or double quoting then the parameter isn't checked for globs eg try:
ls '!(lost+found)'
(the difference between " and ' is variable expansion - it will be expanded when the variable is between " but not when between ')

Find, according to the manual page uses regular expressions (which are not the same as globs) - this are wrong. Find uses globs, but *not the same globs as the shell.*

The standard glob characters are:
* - match 0 or more characters
? - match exactly one character
[] - match one of the characters in the []

Korn Shell (and POSIX shell) add several other globs eg:
!() - find any filename not in the brackets
+() - find one or more filenames in the brackets

Find uses non-korn shell globs - ie only *,? and []...

The best solution for your problem is the following line:

find /path -type f \( -name '*.htm' -o -name '*.html' -o '*.pdf' -o '*.doc' \) -print


Re: find command with wildcards

Thank you very much! All the uggestions works fine. A special thanks to David Lodge for the explanation!

Werner Placke
Deepak Extross
Honored Contributor

Re: find command with wildcards

Since you mention that the output of 'ls' is correct, let me point out that there is a fundamental difference between 'ls' and 'find'.
While 'ls /path' will only list files or directories in /path, find will search recursively in all subdirectories within /path.
For this reason, the list of files retrieved by these two commands may differ not just in format but in content as well.
Be sure of what you are looking for before you decide to use one or the other.
Good Luck.