Search code examples
unixzshglob

why is zsh globbing not working with find command?


I have been using zsh globbing for commands such as:

 vim **/filename
 vim *.html.erb

and so on, but when I type in something like:

 find . -name *mobile*

I get the response:

 zsh: no matches found: *mobile*

Why?


Solution

  • find . -name *mobile* # does not work
    

    vs

    find . -name '*mobile*' # works
    

    The difference is due to the steps that the shell takes when it parses a line. Normally, the shell expands any wildcards it finds before it runs the command. However, the single quotes mark the argument as being a literal, which means that the shell does not perform wildcard expansion on that argument before running the command.

    To demonstrate the difference, suppose you are in a directory with the following files:

    $ tree
    ./
    mobile.1
    dir/
        mobile.2
    

    In the first case, without single quotes, zsh will process as follows:

    • expand the glob, rendering simply mobile.1 (because that is the only matching filename in the current directory
    • pass the result to find, hence:
    find . -name mobile.1
    

    So find will only look for files named literally mobile.1

    In the second form, with single quotes, the entire glob will be preserved and passed to find:

    find . -name *mobile*
    

    Which means that find will look for any filename containing the string "mobile".

    The important thing to note here is that both zsh and find support the same wildcard syntax; by using single quotes, you induce find to handle the wildcards in this case rather than zsh.