ls
solution, including symlinks to directoriesMany answers here don't actually use ls
(or only use it in the trivial sense of ls -d
, while using wildcards for the actual subdirectory matching. A true ls
solution is useful, since it allows the use of ls
options for sorting order, etc.
One solution using ls
has been given, but it does something different from the other solutions in that it excludes symlinks to directories:
ls -l | grep '^d'
(possibly piping through sed
or awk
to isolate the file names)
In the (probably more common) case that symlinks to directories should be included, we can use the -p
option of ls
, which makes it append a slash character to names of directories (including symlinked ones):
ls -1p | grep '/$'
or, getting rid of the trailing slashes:
ls -1p | grep '/$' | sed 's/\/$//'
We can add options to ls
as needed (if a long listing is used, the -1
is no longer required).
Note: if we want trailing slashes, but don't want them highlighted by grep
, we can hackishly remove the highlighting by making the actual matched portion of the line empty:
ls -1p | grep -P '(?=/$)'