
How to List Only Directories in Linux
Listing directories on Linux can be useful when organizing files, managing permissions, and troubleshooting issues. However, sometimes it is necessary to list only directories and not files. In this article, we will discuss different methods to list only directories on Linux and for illustration, I have used Linux Mint 21 distros.
How to list only directories in Linux
Listing directories on Linux can be a beneficial when finding a specific file from a large amount of data, here are some ways to get only directories on Linux:
- Using ls Command with Options
- Using find Command
- Using tree Command
Method 1: Using ls Command with Options
The most straightforward way to list directories on Linux is by using the ls command with specific options. Only directories can be listed using the -d option, and this command will list every directory in the current directory:
ls -d */ |
If you want to list directories in a specific path, you can specify the path after the command:
ls -d /path/to/directory/*/ |
Method 2: Using find Command
The find command is a powerful utility that allows you to search for files and directories based on different criteria. The following command will locate all directories in the current directory and its subdirectories in this situation if we use it to list only directories:
find . -type d |
If you want to exclude hidden directories (directories that start with a dot), use the following command:
find . -type d ! -path '*/.*' |
Method 3: Using tree Command
The tree command is a useful tool that presents directory hierarchies as trees. By default, it displays a list of all the directories, files, and subdirectories in the current directory. To list only directories, use the -d option and to use this command one needs to install it first on Linux using the apt package manager
tree -d |
If you want to list directories in a specific path, you can specify the path after the command:
tree -d /path/to/directory |
Conclusion
In Linux, there are several ways to list only directories using the ls command with options, the find command, and the tree command are all effective ways to achieve this goal. By mastering these commands, you can easily manage and organize directories on your Linux system.