LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2009, 09:43 AM   #1
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 64
What's the difference between these two find commands?


Code:
find . -name *.txt
Code:
find . -name \*.txt
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-27-2009, 10:02 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.

Last edited by colucix; 12-27-2009 at 10:03 AM.
 
Old 12-27-2009, 10:09 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
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.
 
2 members found this post helpful.
Old 12-27-2009, 05:51 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by cola View Post
Code:
find . -name *.txt
Code:
find . -name \*.txt
a better way is just to put double quotes around them.
 
Old 12-28-2009, 01:43 PM   #5
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Original Poster
Rep: Reputation: 64
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]
 
Old 12-28-2009, 01:45 PM   #6
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
That's odd, what version of linux are you using? It works flawlessly on my test system.
 
Old 12-28-2009, 01:53 PM   #7
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by cola View Post
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.*"
 
Old 12-28-2009, 01:59 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by cola View Post
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.
 
Old 12-28-2009, 01:59 PM   #9
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Original Poster
Rep: Reputation: 64
Quote:
Originally Posted by rweaver View Post
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.*
 
Old 12-28-2009, 03:25 PM   #10
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
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.
 
Old 01-08-2010, 02:04 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 739Reputation: 739Reputation: 739Reputation: 739Reputation: 739Reputation: 739Reputation: 739
Merged 2 closely-related threads.
 
  


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
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
what is the difference of the commands ust Linux - Newbie 2 04-29-2008 02:27 AM
what is the difference between these commands ls * and echo * izrafel Linux - General 5 06-12-2007 12:49 PM
what's the difference between the two commands? edenCC Linux - General 4 01-05-2007 09:34 AM
I can't see the difference between the two commands xailer Linux - Newbie 3 11-21-2003 02:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:02 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