LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What's the difference between these two find commands? (https://www.linuxquestions.org/questions/programming-9/whats-the-difference-between-these-two-find-commands-778311/)

cola 12-27-2009 09:43 AM

What's the difference between these two find commands?
 
Code:

find . -name *.txt
Code:

find . -name \*.txt

colucix 12-27-2009 10:02 AM

The difference resides in the way the * character is interpreted by the shell. In the first instance the shell expands the wildcard matching all the files with the ".txt" suffix in the current working directory (if any) and the resulting command is something like:
Code:

find . -name fileone.txt filetwo.txt filethree.txt
this obviously gives an error. If no .txt files are present in the current working dir the wildcard passes intact to the find command and it works as below.

In the second instance the * is protected (escaped) from the shell expansion and the find command actually looks for files ending with .txt under the specified directory tree.

tronayne 12-27-2009 10:09 AM

Well, basically,
Code:

find . -name *.txt
find: paths must precede expression: nerd.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

The back slant "escapes" the asterisk (and thus will find the files you're interested in.

Another way is to enclose your argument in quotes; e.g.,
Code:

find . -name '*.txt'
or
find . -name "*.txt"

You can also use brackets
Code:

find . -name '[0-9][0-9]*.txt
will find every file with two digits as the first part of their names and
Code:

find . -name '[Aa][Bb]*.txt
will find every file with Ab, AB, aB as the first two characters.

You can use the brackets in any position in a file name to isolate a particular pattern.

ghostdog74 12-27-2009 05:51 PM

Quote:

Originally Posted by cola (Post 3805915)
Code:

find . -name *.txt
Code:

find . -name \*.txt

a better way is just to put double quotes around them.

cola 12-28-2009 01:43 PM

Getting message , find command
 
find /etc/ -name crond.*
Code:

find: paths must precede expression: cron.hourly
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


rweaver 12-28-2009 01:45 PM

That's odd, what version of linux are you using? It works flawlessly on my test system.

GooseYArd 12-28-2009 01:53 PM

Quote:

Originally Posted by cola (Post 3807077)
find /etc/ -name crond.*
Code:

find: paths must precede expression: cron.hourly
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


-name only takes a single argument, but crond.* expands into several arguments. -name eats the first match, but find assumes that the subsequent matches are additional paths to search.

I think quoting the wildcard will cause find to do the expansion itself, i.e. -name "crond.*"

colucix 12-28-2009 01:59 PM

Quote:

Originally Posted by cola (Post 3807077)
find /etc/ -name crond.*
Code:

find: paths must precede expression: cron.hourly
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


You already got answers in this other thread of yours. This one reported as duplicate.

cola 12-28-2009 01:59 PM

Quote:

Originally Posted by rweaver (Post 3807081)
That's odd, what version of linux are you using? It works flawlessly on my test system.

I did:
Code:

cd /etc
find /etc -name cron.*

That's why i didn't work.

But from ~/ it works.
Code:

cd ~/
find /etc -name cron.*


rweaver 12-28-2009 03:25 PM

Nod, the previous poster was right, I hadn't realized you were in the /etc directory. Escaping the * should make it work from just about anywhere.

pixellany 01-08-2010 02:04 PM

Merged 2 closely-related threads.


All times are GMT -5. The time now is 03:37 AM.