I came across this article which attempts to explain the find command.It gives an example of

find /tmp -iname "data*.txt"

now this article here states that

Double quotes do not suppress the substitution of words that begin with "$" but they do suppress the expansion of wildcard characters.

Also when I tried the two commands

admin@localhost Downloads$ ls
gallifrey-road-doctors-14437-1366x768.jpg  mydir
admin@localhost Downloads$ find . -iname "*gall*"
admin@localhost Downloads$ find . -iname "*gall*"  -----> A
admin@localhost Downloads$ find . -iname *gall*    -----> B
admin@localhost Downloads$

The output seems to be the same. In what case would they be different ? Why is the double quote not suppressing the expansion of the wildcard in A ? I was under the impression A would not work and only B would work.


In case B:

find . -iname *gall*

The shell will expand *gall* into a list of all files matching that pattern. Since you only have one file in your current directory matching that pattern, this becomes:

find . -iname gallifrey-road-doctors-14437-1366x768.jpg

...so find will search for files matching that exact name. foo-gallifrey-road-doctors-14437-1366x768.jpg would not be found. If you had two or more files that match the glob, you would end up with a malformed find command; I'm pretty sure it would simply exit with an error.

In case A:

find . -iname "*gall*"

Because of the quotes, the shell won't expand the pattern. This just becomes (this is what find sees):

find . -iname *gall*

After that, the find command uses that pattern to search for all files (and directories etc.) matching that pattern. This will find both gallifrey-road-doctors-14437-1366x768.jpg and foo-gallifrey-road-doctors-14437-1366x768.jpg and every file that matches the pattern *gall*.

So: your command goes through the shell (which doesn't expand globs within quotes and strips away the outermost layer of quotes), and then goes through to find, which internally uses the pattern to match filenames.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.