Q: How can I list all the directories in my home folder without listing their contents?

A: Linux systems all come with the ls command, which would appear to be the obvious choice when trying to list things on the command line. However, although it can do what we need, other tools may give us a better or more clean output depending on the situation. Here are some different ways you can list directories only in Linux.  Below are some of the common examples.

List Only Directories Using the ls Command

List directory names under current working directory:

ls -d */

For a long listing:

ls -ld */

Example:

Screenshot of terminal showing the output of ls command using -d switch. Listing only directories.

List Directories Only Using grep

You can also using grep. Although at first there seems to be no advantage, it does give you slightly cleaner output to use in scripts or pipelines.

ls -l | egrep '^d'

or for a ultra clean list of directories:

ls -l | egrep '^d' | cut -d" " -f9

Using the above command would be great if you had to loop through the list of directories for some reason.

Example:

Screenshot of terminal showing output of ls command using grep to show only directories. ls -l | egrep '^d'.

If you use this often, you can create an alias to quickly execute the above command.

Using the find Command to List Only Directories

You can also use the find command, although it will dive into all the directories and also show subdirectories (and keep going). The output can be overwhelming and unnecessary.

For example, in my home directory of my test machine, it finds 140 directories.

$ find . -type d -ls | wc -l
140

You can add the maxdepth option to limit how deep the find command will go. Here we will be using maxdepth 1 just to show the main folders in my home directory.

This also has the added bonus of showing all the hidden directories.

find . -maxdepth 1 -type d -ls

Example:

Example of using the find command to list only directories.

Conclusion

There are many ways to accomplish the same thing in Linux. That is what makes it beautiful and allows sysadmins to each have their own style. Here we covered a few ways to list only directories, but I am sure there are more. Sound off in the comments and let us know your favorite way.