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 going to file a bug to findutils on gnu.org when I saw a notice that asked me whether I knew the difference between these two commands:

find -name *.c    

and

find -name "*.c"

I use find command quite often but I don't think these two have any difference. So I'm curious am I wrong or it's just another GNU guys kidding?

share|improve this question
add comment

2 Answers 2

up vote 11 down vote accepted

In the first example, your shell will first expand the *.c to match all files in the current directory which end in .c.

So, if you have one.c, zwei.c, and tres.c in your directory, your shell will expand this to

find . -name one.c zwei.c tres.c

and find will probably get confused because you're passing a couple extra arguments after -name one.c -- zwei.c and tres.c are not considered part of what you're searching with -name.

In the second example, you're passing the literal string *.c to the -name option of find. This is something that find knows how to deal with -- and probably what you're looking for.

An alternate way to accomplish the same thing would be with a backslash escape:

find . -name \*.c

(Note also that your examples need an argument to tell find where to start the search. This is often just . to indicate the current directory.)

share|improve this answer
2  
GNU find defaults to . as the search path. –  Dennis Williamson Feb 17 '11 at 17:37
    
I did not know that. Another case of me using a tool for so long that I'm not caught up on modern conveniences. –  Doug Harris Feb 17 '11 at 19:14
    
Trye. The GNU version defaults to that. Still, it is a good case to explicitly write the dot. Both for making the code more understandable and for compatibility. –  Hennes Oct 26 '13 at 16:04
add comment

The former will only result in *.c if you have no files that match that in the current directory, otherwise it will expand to those filenames. The latter will always result in the text *.c.

share|improve this answer
    
bash has an option for what it does if *.c does not match. It could evaluate to *.c or nothing (or maybe something else, I can't remember). –  richard Sep 19 '12 at 16:05
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.