views:

159

answers:

4
find . \( -name "_*" -or -newer while2 \) -type f -print

In the linux command above , why _* should be quoted ?

+5  A: 

Why _* should be quoted ?

If it isn't, your shell might expand the * to be substituted with files in the current directory. That's probably not what you wanted here.

John Feminella
Right, because the find command will expand the * glob operator.
ashawley
the problem is that the shell expands it before the find command gets it
Javier
+8  A: 

First, the shell expands all unquoted *'s to match file names in the local directory.

After that, the shell runs the find command with that list of file names.

Generally, you don't want the shell to do '*' globbing in the local directory. Generally, you wind the find command to do filename matching in other directories.

The shell has several other things it does before running a command. $VARIABLE replacement is one of those.

S.Lott
+2  A: 

if you don't quote it, the shell (bash?) would try to expand. if there's any file that starts with '_' on the current directory, you'd get something like:

find . \(-name _somefile _someother _file3 .... -or -newer while2 \) -type f -print

certainly not what you want. i always quote patterns, just to be sure the shell won't mess with them

Javier
When I use find . -name c*,it find fewer matches than find . -name "c*". Why?
MainID
probably you have a singe file that starts with c, and the find command gets you all files with the exact same name, instead of other names that start with c
Javier