This command lists directories in the current path: ls -d */
What exactly does the pattern */
do?
And how can we give the absolute path in the above command (e.g. ls -d /home/alice/Documents
) for listing only directories in that path?
This command lists directories in the current path: ls -d */
What exactly does the pattern */
do?
And how can we give the absolute path in the above command (e.g. ls -d /home/alice/Documents
) for listing only directories in that path?
4 (more) Reliable Options.
An unquoted asterisk
*
will be interpreted as a pattern (glob) by the shell.The shell will use it in pathname expansion.
It will then generate a list of filenames that match the pattern.
A simple asterisk will match all filenames in the PWD (present working directory).
A more complex pattern as
*/
will match all filenames that end in/
.Thus, all directories. That is why the command:
1.- echo.
will be expanded (by the shell) to
echo
all directories in the PWD.To test this: Create a directory (
mkdir
) named like test-dir, andcd
into it:Create some directories:
The command
echo ./*/
will remain reliable even with odd named files:But the spaces in filenames make reading a bit confusing.
If instead of
echo
, we usels
, the shell is still what is expanding the list of filenames. The shell is the reason to get a list of directories in the PWD. The-d
option tols
makes it list the present directory entry instead of the contents of each directory (as presented by default).However, this command is (somewhat) less reliable. It will fail with the odd named files listed above. It will choke with several names. You need to erase one by one till you find the ones with problems.
2.- ls
The GNU
ls
will accept the "end of options" (--
) key.3.-printf
To list each directory in its own line (in one column, similar to ls -1), use:
And, even better, we could remove the trailing
/
:An attempt like this:
Will fail on:
ls -d */
) as already shown above.IFS
.IFS
).4.- Function
Finally, using the argument list inside a function will not affect the arguments list of the present running shell. Simply:
presents this list:
This options are safe with several types of odd filenames.
Actual
ls
solution, including symlinks to directoriesMany answers here don't actually use
ls
(or only use it in the trivial sense ofls -d
, while using wildcards for the actual subdirectory matching. A truels
solution is useful, since it allows the use ofls
options for sorting order, etc.Excluding symlinks
One solution using
ls
has been given, but it does something different from the other solutions in that it excludes symlinks to directories:(possibly piping through
sed
orawk
to isolate the file names)Including symlinks
In the (probably more common) case that symlinks to directories should be included, we can use the
-p
option ofls
:or, getting rid of the trailing slashes:
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:Try this one. this will work on all distros ls -ltr | grep drw
to show folder lists without
/
Here is what I am using
ls -d1 /Directory/Path/*;
Here is what I use for listing only directory names: