How To Used LS With Recursive

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

The ls with recursive option and file name doesn't work.

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.

Answer:

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.

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.