Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcards and double quotes with find -name command

Isn't it true that double quotes (and single quotes) suppress wildcard expansion? If this is so, then why does the following

find -name "d*"

not suppess the wildcard *? That is, the output of the above command are all paths to files and directories that begins with the letter d, as though there was some expansion going on. I was expecting the wildcard to be suppressed, so that it would only output paths to files named literally d*, but it's not.

Does this mean double quotes have exceptions in certain cases?

Thanks!

like image 759
Lostinate Avatar asked Apr 02 '16 16:04

Lostinate


3 Answers

On *NIX platforms the shell is responsible for interpreting and expanding filename wildcards, even before the called program is executed; program will be only able to see expanded filenames.

You can suppress wildcard character expansion by

$ # Using single or double quotes around it
$ find . -name '*'

$ # Escaping it
$ find . -name \*

$ # Or disable the glob expansion (noglob)
$ set -f
$ find . -name *

In case of find its -name parameters expects pattern

  -name pattern
              Base  of file name (the path with the leading directories
              removed) matches shell pattern pattern...
              Don't forget to enclose the pattern in quotes in order to
              protect it from expansion by the shell.

So even if wildcard character has not been expanded by shell, find would use your expression while searching for matching files.

like image 112
luka5z Avatar answered Oct 20 '22 00:10

luka5z


Quotes suppress wildcard expansion by the shell, which means in this case the argument to find -name will be a literal d*.

Without quotes, the shell would be the one to expand the wildcard, in which case d* would expand to a list of all names within the current directory starting with d, or to d* in the case there are no such files.

The -name search criterium takes a pattern as argument with the same syntax then shell globs. In this case the the pattern is intended to be interpreted by find, not by the shell, so quotes are needed.

To search for a literal *, you can enclose it in square brackets:

find -name "d[*]"

or escape it with a backslash:

find -name 'd\*'
like image 23
mata Avatar answered Oct 19 '22 23:10

mata


To clarify the shell expansions,

way 1: use these patterns with echo command as argument.

way 2: before executing find command,execute the set -x to enable to see which arguments are passed to the command "find" (or any other commands).

like image 28
purushothaman poovai Avatar answered Oct 20 '22 00:10

purushothaman poovai