Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was using

find -iname *prib*

and getting only one result "./prib09jack.pdf" with

find -iname "*prib*" 

I get many more results that I was looking for such as

./Dir1/PRIB09/prib09jackTFF1.pdf

and many more, but why did it matter that the quotation marks were around "prib"?

Best.

share|improve this question
    
Did you launch the command from the same directory and got different answers? What was the whole find command structure you used as there are many options which affect the outcome. –  dtlussier Aug 26 '10 at 16:27
    
I was in the same directory for each use changing only the quotations around the search string. –  Vass Aug 26 '10 at 16:37
add comment

3 Answers

up vote 9 down vote accepted

If there is a file (or files) matching *prib* in the current directory when the find command is run, the shell will expand the wildcard to match the file name(s) first before handing the arguments to find, and result in a different command being run than the user expects.

For example,

$ ls foo*
foobar

$ find -iname foo*
./foobar

$ find -iname "foo*"
./foobar
./dir/foobz
./dir2/fooblat

In short, Quotes delay wildcard interpretation for the find command to perform. Lack of quotes expands immediately.

share|improve this answer
    
wow, that was deep. I can't say that I completely understood it. So should I always be using the quotes in similar situations? What you said sounded like if it encounters a string matching a form of the wildcard further searches follow this initial wildcard expansion. And that the quotes stop this from happening. (if Im not mistaken the man page says nothing to avoid such a mistake). –  Vass Aug 26 '10 at 16:55
3  
You'd find it explained in the bash man page, not the find man page, because quoting is handled by the shell. Basically (to repeat what kmarsh said), if you use *prib* without quotes, bash looks for files in the current directory with names matching that pattern and replaces *prib* with the names of the files before invoking find. If you use quotes, bash leaves the *prib* alone. –  David Z Aug 26 '10 at 17:02
    
thanks a ton, I even understood that! –  Vass Aug 26 '10 at 17:09
2  
@Vass: You can use echo *prib* and such to see what bash does to the arguments before it passes them to the command. echo itself just prints it's arguments unchanged, so it's output tells you to what bash expanded the *prib*. –  sth Aug 26 '10 at 17:37
1  
@sth zaw. Thanks, alot. that was eye opening. Thanks alot for bothering to tell me this. Its like there is a whole other dimension to the command line that I was unaware of. –  Vass Aug 26 '10 at 20:38
show 1 more comment

The issue is that your shell is expanding the wildcard *'s before handing them to find. You want find to get them, so you need to escape them. The quotes do that, or you could do

find -iname \*iprib\*

and it would work as well. The key thing is, you want the wildcards to make it to find uninterpreted by anything else along the way (like your shell).

share|improve this answer
add comment

Not sure what version of find (or even what shell) you are using, but I have always had to add a path before pattern:

find . -iname "*prib*"

Would recurse from current directory. (".")

If you used:

find /etc -iname "*prib*"

it would recurse the /etc directory.

The following would limit 'find' to 2 directory levels from search path ( . )

find . -maxdepth 2 -iname "*prib*"

Man Page:

-iname pattern

Like -name, but the match is case insensitive. For example, the patterns fo*' andF??' match the file names Foo',FOO', foo',fOo', etc. In these patterns, unlike filename expansion by the shell, an initial '.' can be matched by '*'. That is, find -name *bar will match the file `.foobar'. Please note that you should quote patterns as a matter of course, otherwise the shell will expand any wildcard characters in them.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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