LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-19-2009, 07:51 AM   #1
Matey
Member
 
Registered: Jan 2009
Posts: 114

Rep: Reputation: 17
Question about "find" command (Ubuntu/Deb)


Hello, I have a very simple question but I cannot get much use put of the --help or man files!(too damm-many brackets [[]] 2 confusing to noob)..

1- Is find command case sensitive? Like if I am looking for a file Foo can I type find -name foo or do I have to say Foo?
2-Can I use wild-card or * in my find?
for instance I want to find foo but I say:
find -name fo*
can it read the * as a "dont care" statement?

Thank You in advance!
 
Old 03-19-2009, 07:57 AM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,527

Rep: Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898
1 yes
2 yes
 
Old 03-19-2009, 08:01 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Linux (and all Unix-like systems) are typically case-sensitive.

In man pages, those brackets are there for a reason---once you get used to it, you'll find it helpful.

In Bash, the "*" is normally a wildcard. In certain commands which use regular expressions, it has a different meaning. The man page (sorry) will tell you all the details.

In find, it's a wildcard, but I've discovered I often need to to this: (Suppose I am looking in /usr/lib for a filename containing "foo")

find /usr/lib -name "*foo*"

I'm not totally clear on why the quotes are needed, but it works.

I recommend reading the Bash Guide for Beginners (free at http://tldp.org)
 
Old 03-19-2009, 10:43 AM   #4
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Quote:
Originally Posted by pixellany View Post
find /usr/lib -name "*foo*"

I'm not totally clear on why the quotes are needed, but it works.
Well I can quote you the reason I gave you in an earlier thread. I'm pretty sure this is correct, was it not entirely clear?

Quote:
Originally Posted by openSauce View Post
Without quotes, the shell expands *foo* to either:
  • all files in the current directory with "foo" in the name
  • or the literal string *foo* if no such files exist.
It then passes the result to find. The latter is what you want, and you can force the shell not to expand the *'s by using quotes.
Matey: use -iname rather than -name for a case insensitive search.

e.g.
Code:
find /home/user -iname "*foo*"
will find all files in your home dir with "foo" (case-insensitive) somewhere in the name.

I seem to remember there's a good article or two on linux-mag.com on using find, but the site seems to be down at the mo so I can't find it and link to it.

EDIT: I think these are the ones I was thinking of. Unfortunately you need to register to see them, but it's free.
http://www.linux-mag.com/id/1157
http://www.linux-mag.com/id/6981

Last edited by openSauce; 03-19-2009 at 10:46 AM.
 
Old 03-19-2009, 10:18 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by openSauce View Post
Well I can quote you the reason I gave you in an earlier thread. I'm pretty sure this is correct, was it not entirely clear?

Without quotes, the shell expands *foo* to either:

* all files in the current directory with "foo" in the name
* or the literal string *foo* if no such files exist.

It then passes the result to find. The latter is what you want, and you can force the shell not to expand the *'s by using quotes.

Not remembering this from you---but no matter.

I think you've just cleared a mystery for me.....Much of the time, find works just fine without quotes. If I read you correctly......it works like this:
If the string after -name is not in the current directory, then the command works OK.
If there is anything in the current directory containing the search string, then it is passed to the find command.

Suppose there are 2 files containing the search string---they'll get passed in a list with a newline, n'est-ce pas? THEN what does find do??

All this aside, THANK YOU for pointing out what is happening.
 
Old 03-20-2009, 12:53 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
iirc, you use single quotes ( '*foo*' ) so that they are interpreted by the find cmd itself, not the current shell, otherwise you could get 'too many args ...' error.
 
Old 03-20-2009, 01:05 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by pixellany View Post
Not remembering this from you---but no matter.

I think you've just cleared a mystery for me.....Much of the time, find works just fine without quotes. If I read you correctly......it works like this:
If the string after -name is not in the current directory, then the command works OK.
If there is anything in the current directory containing the search string, then it is passed to the find command.

Suppose there are 2 files containing the search string---they'll get passed in a list with a newline, n'est-ce pas? THEN what does find do??

All this aside, THANK YOU for pointing out what is happening.
If there is more than one match for an entry like -name cmd*

That will expand the command to `-name cmd001 cmd002, ...' . This will cause an error. It's not the same as `-name cmd001 -name cmd002' which would be OK.
If there is only one match, then the cmd* will be expanded to a literal which would not cause a problem.

You could have
find */ -name "aname*"

Which would expand to something like:
find dir1/ dir2/ dir3/ -name "aname*"

This would be OK. You can explicitly have move then one directory in your find command. For example:
$ find /bin /usr/bin /usr/local/bin -name "ffplay*"
/usr/local/bin/ffplay


This can be a lot easier than trying to prune directories from a search.
 
  


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
Using a single "Find" Command to find files bases on multiple criteria roboxooo Linux - Newbie 6 01-15-2009 05:13 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
What does "2>" do when added to "find" command? O(V)eGA_l2el) Fedora 8 08-06-2007 03:25 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
question on "find" command jcosenzo Linux - General 1 04-18-2005 03:39 PM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration