ls -R on Ubuntu does not recursively find files matching the pattern in sub directories


ls -R – Lists all files recursively.

ls a* – Lists all files matching pattern “a*”.

However: ls -R a* does not list all files in sub directories matching patter “a*”.

Why it happens

This happens because of specific implementation of linux.  It’s the shell (ie Bash) that expands the “A*” to the names of all files/directories in the current directory that start with “A”, prior to passing the expansion result to “ls”.

So, let’s say I have, in my current directory: A1.txt A2.txt A3 – a subdirectory Than, “ls A*”, will actually be performed as “ls A1.txt A2.txt A3″ (“ls” gets 3 input parameters, not just the ‘A*’).

How to Fix

You cannot use ls to do recursive selective file listing. Instead use command “find”.

use: find . -name ‘A*’


Be Sociable, Share!
This entry was posted in Open Source. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>