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 Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















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