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?

link|improve this question
feedback

2 Answers

up vote 6 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 was 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.)

link|improve this answer
1  
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
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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