LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-14-2005, 01:59 PM   #1
hpladd
Member
 
Registered: Feb 2003
Distribution: Kubuntu
Posts: 142

Rep: Reputation: 15
Wild Cards using the find command -- grrr


I'm baffled by the find command's response to the * (asterisk) wild card. Would anyone be kind enough to explain the results of the three searches below?

[root@localhost /]# find / -iname eddie
/eddie
[root@localhost /]# find / -iname eddie?
/eddie2
[root@localhost /]# find / -iname eddie*
find: paths must precede expression
Usage: find [path...] [expression]

But if I move the files to my home directory:

[root@localhost /]# find /home/nunya -iname eddie*
/home/nunya/eddie
/home/nunya/eddie2

Perhaps is it a precaustion against entering commands such as rm / *
 
Old 07-14-2005, 02:05 PM   #2
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
You need to run command as follows:
Code:
find / -iname "eddie*"
 
Old 07-14-2005, 02:28 PM   #3
hpladd
Member
 
Registered: Feb 2003
Distribution: Kubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
Thanks much nixcraft,

Is the explanation of why the quotes are needed a long one? Could you point me in the right direction to research this further? A topic of research perhaps?

Thanks again.
 
Old 07-14-2005, 02:56 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
The quotes are necessary to prevent the shell from expanding them too early.

When you execute a command using a wildcard (such as rm *), the shell expands the asterisk before calling rm. So if you had a directory containing three files (file1, another_file, third_file), then bash expands the command to:
Code:
rm file1 another_file third_file
The rm command never, ever, ever sees the asterisk; it only sees a list of files and directories.

So take that and apply it to your command:
Code:
find / -iname eddie*
You have two directories that match that pattern: eddie and eddie2. The shell expands the wildcard, so the final command looks like this:
Code:
find / -iname eddie eddie2
The first match (eddie) is applied to the -iname option. The second match (eddie2) is now orphaned. So find thinks you're trying to specify another path for it to look in. So it errors out.

You use the double quotes ("eddie*") to prevent the shell from expanding the asterisk. By doing that, the find command sees the asterisk, and interprets it as you intended.
 
Old 07-14-2005, 04:00 PM   #5
hpladd
Member
 
Registered: Feb 2003
Distribution: Kubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
Thanks much Dark Helmut,

I think I understand -- the find search can't be completed the because the * wild card is expanded, and the return from the expansion process is used as a part (condition) of the find command. However the return's syntax is wrong -- quite wrong -- and can not be understood by the find command.

I'm confused as to why this situation only applies to the / directory. If you don't mind my asking, why does following command work?

Code:
find /home/nunya -iname eddie*
Thanks again.
 
Old 07-14-2005, 04:39 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
There's no relation to the root directory ( / ) and the problem you were seeing. It's the directory you run the command that influences it.

In your original command, you executed the find command from the root directory. There were two directories at the root level that matched the eddie* pattern: /eddie and /eddie2. If you executed the exact same command from /home, you wouldn't have encountered the same problem. The reason is because (I assume) there are no eddie* directories inside /home. In other words, the eddie* pattern would not match anything in the directory you launched the command from, and that means eddie* is replaced by an empty string. The final command would look something like:
Code:
find / -iname
That might cause the find command to error out because there is no argument for iname, or it might just ignore that option... dunno which... haven't tried.
 
Old 07-14-2005, 04:55 PM   #7
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Actually, if the shell can't expand an expression, it leaves the expression verbatim. This is why the following occurs:
Code:
$ ls non-existant*
ls: non-existant*: No such file or directory
If the shell replaced it with a blank, ls would list all the files in the directory (like ls by itself). This would lead to some very confusing behavior.

If you want to play around with seeing how the shell expands expressions, use echo.
Code:
$ echo non-existant*
non-existant*
$ ls
test1 test2
$ echo test*
test1 test2
$ echo *
test1 test2
$ echo "test*"
test*
(By the way, this is one way to list files in a directory WITHOUT spawning another process... it's useful to check for a rootkit with a hacked version of ls... )

If more explanation is needed, please let me know and I'd be quite glad to expand on this more.
 
Old 07-14-2005, 05:56 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Good call Matir. As I was writing it, I knew there was something that wasn't quite right, because I'd seen the "blah*: no such file or directory" message you quoted before.

My apologies if that caused confusion.
 
Old 07-14-2005, 06:03 PM   #9
hpladd
Member
 
Registered: Feb 2003
Distribution: Kubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
Thanks again folks. I recreated the situation and copied and pasted the text from my terminal (below).

The steps it details are: 1) Delete all files named eddie 2) double check that eddie files are gone. 3) create the eddies. 4) list the eddies. 5) try to find the eddies -- and fail 6) move the eddies. 7) try to find the eddies again (using the same command as before) and succeed.


Code:
[root@localhost /]# find / -iname "*eddie*" -exec rm -f {} \;
[root@localhost /]# find / -iname "*eddie*"
[root@localhost /]# vi eddie; vi eddie2
[root@localhost /]# ls
bin   dev    eddie2  home    lib         misc  opt   root  tmp  var
boot  eddie  etc     initrd  lost+found  mnt   proc  sbin  usr
[root@localhost /]# find / -iname eddie*
find: paths must precede expression
Usage: find [path...] [expression]
[root@localhost /]# mv /eddie /home ; mv /eddie2 /home
[root@localhost /]# find / -iname eddie*
/home/eddie
/home/eddie2
[root@localhost /]#
 
Old 07-14-2005, 06:18 PM   #10
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Yep, the behavior is exactly as expected.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
More Players Supporting Wild-Cards? chime Linux - General 0 12-01-2004 11:21 AM
How to find Packet driver interrupt for ethernet cards palanisaravanan Linux - Networking 1 12-29-2003 02:18 PM
How to find Packet driver interrupt for ethernet cards palanisaravanan Linux - Hardware 0 12-29-2003 09:25 AM
where to find drivers for wireless cards? gmoney Linux - Wireless Networking 10 10-20-2003 07:32 PM
how RH7.3 auto find and loads cards (FC specifically) Nitemare Linux - Hardware 0 02-17-2003 01:00 AM


All times are GMT -5. The time now is 10:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration