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?
*/
is a filename matching pattern that matches directories in the current directory.To list directories only, I like this function:
Put it in your .bashrc.
Usage examples:
NOTE: it will break if you use the
-i
option. Here is a fix for that:To answer the original question,
*/
has nothing to do withls
per se; it is done by the shell/Bash, in a process known as globbing.This is why
echo */
andls -d */
output the same elements. (The-d
flag makesls
output the directory names and not contents of the directories.)*/
is a pattern that matches all of the subdirectories in the current directory (*
would match all files and subdirectories; the/
restricts it to directories). Similarly, to list all subdirectories under /home/alice/Documents, usels -d /home/alice/Documents/*/
For listing only directories:
for listing only files:
or also you can do as: ls -ld */
For all folders without subfolders:
For all folders with subfolders:
Using Perl: