LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-10-2012, 01:37 PM   #1
prasunjit
LQ Newbie
 
Registered: Dec 2012
Posts: 17

Rep: Reputation: Disabled
* expansion


Hello

i have two text files in the current directory: lserror.txt lsouput1.txt

while executing the below command to find files without double quotes as "*.txt" it is throwing error
find . -type f -name *.txt
But the below command without double quotes as "*.txt" is giving correct result
ls *.txt

pls explain
 
Old 12-10-2012, 01:43 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to pass a literal asterisk to the find command, otherwise the shell expansion brings to a wrong command line. Since the .txt files are in the current working directory, the shell substitutes their name in the command line, givin'
Code:
find . -type f -name lserror.txt lsouput1.txt
which is wrong. On the other hand, two (or more) arguments are accepted by the ls command, so that the shell substitution
Code:
ls lserror.txt lsouput1.txt
bring to a correct command line. To pass a literal * to find, either escape it or use single or double quotes:
Code:
find . -type f -name \*.txt
find . -type f -name '*.txt'
find . -type f -name "*.txt"
All the lines above are equivalent.
 
1 members found this post helpful.
Old 12-11-2012, 11:55 PM   #3
prasunjit
LQ Newbie
 
Registered: Dec 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Smile

@colucix..thnks for the stuff..
But it seems all the below commands are going to recursion giving text files within sub directories as well...

f u can help

Quote:
Originally Posted by colucix View Post
You have to pass a literal asterisk to the find command, otherwise the shell expansion brings to a wrong command line. Since the .txt files are in the current working directory, the shell substitutes their name in the command line, givin'
Code:
find . -type f -name lserror.txt lsouput1.txt
which is wrong. On the other hand, two (or more) arguments are accepted by the ls command, so that the shell substitution
Code:
ls lserror.txt lsouput1.txt
bring to a correct command line. To pass a literal * to find, either escape it or use single or double quotes:
Code:
find . -type f -name \*.txt
find . -type f -name '*.txt'
find . -type f -name "*.txt"
All the lines above are equivalent.

Last edited by prasunjit; 12-12-2012 at 12:12 AM.
 
Old 12-12-2012, 12:30 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
find is recursive by its nature, but you can limit the level of recursion (that is how much it descends into subdirectories) using -maxdepth (and eventually the related -mindepth) expression. In order to limit the search to the current working directory, you can try:
Code:
find . -maxdepth 1 -name \*.txt
Anyway, in this case is more convenient to use
Code:
ls *.txt
unless you want to use some more strict search criteria or apply some action on the results by means of the find command facilities.
 
1 members found this post helpful.
Old 12-12-2012, 07:10 AM   #5
prasunjit
LQ Newbie
 
Registered: Dec 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thanks a lot....this was really helpful.

Quote:
Originally Posted by colucix View Post
find is recursive by its nature, but you can limit the level of recursion (that is how much it descends into subdirectories) using -maxdepth (and eventually the related -mindepth) expression. In order to limit the search to the current working directory, you can try:
Code:
find . -maxdepth 1 -name \*.txt
Anyway, in this case is more convenient to use
Code:
ls *.txt
unless you want to use some more strict search criteria or apply some action on the results by means of the find command facilities.
 
  


Reply



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
Some help with variable expansion please jakev383 Programming 6 12-06-2011 01:35 PM
macro expansion in vi editor cleopard Linux - General 0 05-02-2008 07:35 AM
Expansion Port 3 berlingo Linux - Newbie 2 09-21-2007 11:35 PM
Does anyone have the expansion pack for 5.2? solid7 AIX 14 04-26-2005 12:18 PM
Raid 5 Expansion Ruckuss Linux - General 3 01-05-2002 05:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:56 AM.

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
Open Source Consulting | Domain Registration