LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-04-2007, 02:05 AM   #1
FromFPan2Fire
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu 6.06 LTS Dapper Drake
Posts: 5

Rep: Reputation: 0
ls with recursive option and file name doesn't work


I am having a very basic problem with the ls command.

Version: Ubuntu; 6.06 LTS (Dapper D)

Entering ls command from a terminal window:

When I do "ls -R" I get what I expect, a recursive list of all files

When I do "ls a*" I also get what I expect - list of files starting with "a" in the current directory

But when I combine them: "ls -R a*" looking for a list of all files starting with a in the current directory and all subdirectories, it appears to ignore the "-R" option, only returning results from the current directory.

For another example, I know I have files in a subdirectory beginning with W; But when I do "ls -R W* " it says No Such File or Directory; If I do ls -R then I can see files starting with W.

What is it that I'm not getting?

Thank You
 
Old 01-04-2007, 03:06 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
Originally Posted by FromFPan2Fire
For another example, I know I have files in a subdirectory beginning with W; But when I do "ls -R W* " it says No Such File or Directory; If I do ls -R then I can see files starting with W.
Actually, it's the shell (ie Bash) that expands the "W*" to the names of all files/directories in the current
directory that start with "W", prior to passing the expansion result to "ls".

So, let's say I have, in my current directory:
W1.txt
W2.txt
W3 - a subdirectory
Than, "ls W*", will actually be performed as "ls W1.txt W2.txt W3" ("ls" gets 3 input parameters, not just the 'W*').

Following this idea, then you can't locate files with names starting with W in all subdirectories by
simply calling
Code:
ls -R W*
As this is performed as follows:
1. The shell expands "W*" to all names of files/directories IN THE CURRENT DIRECTORY that have names starting with "W". If there aren't any, you'll get "No such file or directory".
2. If the shell did find some files/directories with W, it'll pass them all to "ls" as input parameters.
3. "ls -R" is performed for each of the parameters "ls" gets from the shell (if there are any).

To find all files starting with W in a recursive way (ie in the current directory and all subdirectories), you
need to use "find", not "ls". Example:
Code:
find . -name 'W*'
You may add "-type f" option to find to restrict the output to regular files (ie no directories, symbolic links, fifo pipes, device files, etc).
"-maxdepth n" restricts the number of recursively searched levels to "n".

For more details on "find" and "ls", please read their respective man pages.
 
1 members found this post helpful.
Old 01-04-2007, 04:05 AM   #3
FromFPan2Fire
LQ Newbie
 
Registered: Jan 2007
Distribution: Ubuntu 6.06 LTS Dapper Drake
Posts: 5

Original Poster
Rep: Reputation: 0
ls and find command clarified

Thank You
I appreciate your assistance.
That's what I was looking for.
 
Old 01-04-2007, 05:07 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
You're welcome.
 
Old 08-15-2009, 09:07 AM   #5
scm2
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 0
thanks, that's just what I was trying to do as well!

But I also have another similar question as well...

How do I grep, but only in a certain file type?

For example, if I try this I get thousands of results (it's a small common word in a very big directory structure)

grep 'common word' * -R

so I want to be able to do something like this:

grep 'common word' *.csv -R

But this doesn't work, probably for the same reasons as above. So is there a way to do this?
 
Old 08-16-2009, 05:10 AM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,061

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
OK, just this once....

You shouldn't do this
  • its probably 'necroposting'
  • the new question isn't sufficiently closely related to the original to be a minor clarification

so better to start a new thread.

But as you have dragged me into your necropost to even tell you this, I might as well give you my contribution.

timmeke's explanation and way of deasling with the original question was the more elegant, more classy, way of dealing with it, but a 'brute force and ignorance' person like myself could also consider:

ls -R | grep filename

(where filename is, well the filename, or file name fragment of interest, and if you want to ignore case, then that would be grep -i rather than grep (see 'man grep' ))

This then leads on to the possibility of using grep several times. I'll generalise the problem a little bit and asume that you are looking for all files that are variants of squid confinfiguartion files anywhere on your system (and that you have locate/slocate/... installed and that the database is updated...you can do this manually with updatedb but your distro probably schedules this while you are asleep)

locate squid | grep -i conf

will catch all files that contain the substrings 'squid' and 'conf' (or CONF or Conf or config...) and if you want to only see the ones that also contain .bak, or some variation thereof (replace .bak with .old or whatever you use, if you are adding extensions to old files manually)

locate squid | grep -i conf | grep -i .bak

or all files in the user1 tree with ~ in the title (which are almost certainly the automatic backup files made by a text editor when user1 edits files)

locate user1 | grep ~

HTH (and don't do it again, as I don't want the mods on my tail )
 
Old 08-16-2009, 07:16 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Might as well continue by pointing out grep has many switches/options, inc -r (-R) = recursive
http://linux.die.net/man/1/grep
 
Old 01-05-2010, 10:21 AM   #8
roberthallphoto
LQ Newbie
 
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
Thank you all for your answers.
 
  


Reply

Tags
file, ls, name, recursive


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
How to do recursive file delete using specifier (*.tmp) from nested directories? Arodef Linux - General 3 11-11-2009 07:49 AM
Perl file handler in recursive function enemorales Programming 4 02-02-2009 03:20 PM
Mouse scrolly doesn't work, even though added axis option in config file. RHLinuxGUY Slackware 8 07-22-2005 11:33 PM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM
recursive file permissions does not change new files in same directory PAB Linux - Newbie 2 03-08-2004 12:27 PM


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