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? :scratch:
directory that start with "W", prior to passing the expansion result to "ls".
So, let's say I have, in my current directory:
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
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:
"-maxdepth n" restricts the number of recursively searched levels to "n".
For more details on "find" and "ls", please read their respective man pages.
ls and find command clarified
I appreciate your assistance.
That's what I was looking for.
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?
OK, just this once....
You shouldn't do this
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 :redface: )
Might as well continue by pointing out grep has many switches/options, inc -r (-R) = recursive
Thank you all for your answers.
|All times are GMT -5. The time now is 08:35 AM.|