Difference between find -name with and w/o quotes?

3

1

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?

fwonce

Posted 2011-02-17T16:49:13.053

Reputation: 55

Try the same in Fish, and it will teach you a lesson – you have been a bad boy.

– user2394284 – 2016-12-22T20:44:15.130

Answers

11

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.)

Doug Harris

Posted 2011-02-17T16:49:13.053

Reputation: 23 578

2GNU find defaults to . as the search path. – Paused until further notice. – 2011-02-17T17:37:26.473

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 – 2011-02-17T19:14:19.323

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 – 2013-10-26T16:04:42.697

2

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.

Ignacio Vazquez-Abrams

Posted 2011-02-17T16:49:13.053

Reputation: 100 516

Try shopt -s failglob or shopt -s nullglob for safer behaviour. – user2394284 – 2016-12-22T20:52:51.997

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). – ctrl-alt-delor – 2012-09-19T16:05:59.097