ls command to list only directories

By default when you run the ls command, it will show you all the files and directories in the directory you run this command.

If you want to only display the list of all the directories, you can make use of the -d option.

-d: Directories are listed as plain files (not searched recursively).

Note it will not work just by using the -d option, you would also need to add */

% ls -d */
/Applications	/System		/Volumes	/cores
/etc		/opt		/sbin		/usr
/Library	/Users		/bin		/dev		
/home		/private	/tmp		/var

The * is the wildcard to match any string and / to indicate that only directories should be listed.

If you also want to see hidden files you may add -a flag and -l to display it as a list.

% ls -lad */
ls command to list only directories

Facing issues? Have Questions? Post them here! I am happy to answer!

Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap