Why backslash before asterisk in `find`?
An unquoted glob would be expanded by the shell before find
is executed. (Refer to Filename Expansion in the manual.)
So saying:
find . -name *.pyc -delete
would actually execute:
find . -name file1.pyc file2.pyc file3.pyc -delete
assuming there were 3 .pyc files in the current directory and result in an error instead.
A backslash makes the shell pass the glob to find
, i.e. it acts as if *.pyc
were quoted.
Ideally, you should be quoting a glob:
find . -name '*.pyc' -delete
Before your shell issues the find
command, it will do various expansions. Doing so, it also processes special characters (or, characters with special meaning), where *
is a wildcard – a globbing character. This is the so-called filename expansion.
Say you have two files in your directory:
foo.pyc
bar.pyc
Then *.pyc
would expand to both names. So if you write:
find . -name *.pyc -delete
then the shell will actually call:
find . -name foo.pyc bar.pyc -delete
which does not make a lot of sense, because you can only have one argument for -name
. That's why you need to escape the special character to prevent it from being interpreted by the shell. You do that by backslash-escaping, or alternatively, quoting it.