LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Confused by command "find" (https://www.linuxquestions.org/questions/linux-newbie-8/confused-by-command-find-500686/)

chen666 11-11-2006 05:02 PM

Confused by command "find"
 
Hi, greetings from a newbie. I have been using Linux for 5 years, but not really good at anything :)

I am getting really confused by the command (GNU) find. Depending on the files existing in the current directory, "find" gave me different responses. Could anyone please give me a hint? I am using Debian, Sarge5. Thanks a lot!

-------------------
jchen:~/tmp$ ls -l
total 4
-rw-r--r-- 1 jchen jchen 13 2006-11-11 17:47 test.txt
-rw-r--r-- 1 jchen jchen 0 2006-11-11 17:41 test.txt~
jchen:~/tmp$
jchen:~/tmp$ find -name *test*
find: paths must precede expression
Usage: find [path...] [expression]
jchen:~/tmp$
jchen:~/tmp$ rm -f ./test.txt~
jchen:~/tmp$
jchen:~/tmp$ find -name *test*
./test.txt
jchen:~/tmp$
jchen:~/tmp$ touch ./test2.txt
jchen:~/tmp$
jchen:~/tmp$ find -name *test*
find: paths must precede expression
Usage: find [path...] [expression]
jchen:~/tmp$

acid_kewpie 11-11-2006 05:11 PM

yeah that caught me for a long time too... essnetially the *'s are doing filename expansion in the shell, so it just screws up the logic of what you're trying to do. put single quotes around that *test* and it should work as you'd expect it to do, as find itself will use the *'s not the underlying bash process.

dxqcanada 11-11-2006 05:22 PM

-name pattern

man page says to enclose the peattern in quotes in order to protect if from expansion by the shell

Code:

find -name "*test*"

theNbomr 11-11-2006 05:27 PM

Just because I try to be explicit about everything, I generally do
Code:

find ./ -name "*test*"
But, sometimes I want to do
Code:

find /some/directory/of/interest -name "*fub*"
And to clarify, in case acid_kewpie's answer didn't make complete sense to you, the difference between *test* and "*test*" is what ends up being seen by find. Without the quotes, bash (or whatever shell, generally) will expand "*test*" into a whole list of matching files, and hands them to find. Find only knows how to deal with one filespec, and complains or does something unexpected. By putting the quotes on your filespec, bash just hands the string '*test*' to find. find sees it as a single argument, and recognizes that it must do the wildcard expansion.

This is a subtle point that many people don't immediately grasp, and once understood can help make sense out of a lot that previously did not.

--- rod

chen666 11-11-2006 05:43 PM

Thanks a lot, guys! Learned more than what I asked. Should be coming here more later on :)


All times are GMT -5. The time now is 09:10 AM.