1. Overview
In this tutorial, we’ll discuss how to list only directories for a given path in Linux. There are several ways to list only directories in Linux and we will cover a few of them here.
2. Using ls Command
The ls command is one of the most popular commands to list the contents of the directory. To only list directories, we can use this command with the ‘-d‘ option which only lists the directories in the current path and not their contents or sub-directories:
$ ls -d */
arch/ Documentation/ include/ lib/ scripts/ usr/
block/ drivers/ init/ mm/ security/ virt/
certs/ firmware/ ipc/ net/ sound/
crypto/ fs/ kernel/ samples/ tools/
3. Using dir Command
The dir command lists the directory contents. When we use this command with the ‘-d‘ option, it will only show the details about the directories in the current path:
$ dir -d */
drwxr-xr-x 33 HARDIK users 4096 Feb 12 2018 arch/
drwxr-xr-x 3 HARDIK users 4096 Feb 12 2018 block/
drwxr-xr-x 2 HARDIK users 200 Feb 12 2018 certs/
drwxr-xr-x 4 HARDIK users 4096 Feb 12 2018 crypto/
drwxr-xr-x 122 HARDIK users 8192 Feb 12 2018 Documentation/
... ... ... ... ... ... ...
4. Using find Command
The find command searches for the file starting from the given starting point in a directory hierarchy. We can pass the expressions to this command to narrow down the search criteria. For instance, to list the file type directory, we can use ‘type‘ with the directory option ‘d‘. Also to avoid listing all the contents of subdirectories, we can limit the search level with the help of ‘maxdepth‘:
$ find . -maxdepth 1 -type d
.
./Documentation
./arch
./block
./certs
./crypto
...
5. Using tree Command
The tree command lists the contents of the directory in a depth indented tree-like format. We can use this command to show only directories with the help of the option ‘d‘. However, this may list all subdirectories and their contents, too. So we can use the level option (L) which limits the maximum display depth of the directory tree:
$ tree -d -L 1
.
├── arch
├── block
├── certs
├── crypto
├── Documentation
...
6. Using echo Command
As we know that the echo command displays the string back to the standard output that we passed to it. We can also display the content of the directory using this command. If we try the ‘echo *****‘ command, it will show all the contents of the current directory. Moreover, to only list directories we can use it like this:
$ echo */
arch/ block/ certs/ crypto/ Documentation/ drivers/ firmware/
fs/ include/ init/ ipc/ kernel/ lib/ mm/
net/ samples/ scripts/ security/ sound/ tools/ usr/
7. Using grep with Other Commands
When we list the contents of the directory with the long listing option of ls command (ls -l) or dir command, we can see in the first column that regular files are shown as ‘–‘ and directories are shown as ‘d‘:
$ ls -l
total 708
drwxr-xr-x 33 HARDIK users 4096 Feb 12 2018 arch
drwxr-xr-x 3 HARDIK users 4096 Feb 12 2018 block
drwxr-xr-x 2 HARDIK users 200 Feb 12 2018 certs
-rw-r--r-- 1 HARDIK users 18693 Feb 12 2018 COPYING
-rw-r--r-- 1 HARDIK users 98556 Feb 12 2018 CREDITS
... ... ... ... ... ... ...
So from this output, we can filter directories with the help of the grep tool. Grep is just a Linux / Unix command-line tool used to search for a string of characters, so the option ‘^d‘ filters those strings that start with ‘d’.
7.1. Using grep with ls Command
If we can list all files and directories with ‘ls -l‘ and pass them to grep, we can filter them to keep only directories like this:
$ ls -l | grep '^d'
drwxr-xr-x 33 HARDIK users 4096 Feb 12 2018 arch
drwxr-xr-x 3 HARDIK users 4096 Feb 12 2018 block
drwxr-xr-x 2 HARDIK users 200 Feb 12 2018 certs
drwxr-xr-x 4 HARDIK users 4096 Feb 12 2018 crypto
drwxr-xr-x 122 HARDIK users 8192 Feb 12 2018 Documentation
... ... ... ... ... ... ...
7.2. Using grep with dir Command
We can use the grep command with the dir command as well in a similar way as shown above to list directories:
$ dir | grep '^d'
drwxr-xr-x 33 HARDIK users 4096 Feb 12 2018 arch/
drwxr-xr-x 3 HARDIK users 4096 Feb 12 2018 block/
drwxr-xr-x 2 HARDIK users 200 Feb 12 2018 certs/
drwxr-xr-x 4 HARDIK users 4096 Feb 12 2018 crypto/
drwxr-xr-x 122 HARDIK users 8192 Feb 12 2018 Documentation/
... ... ... ... ... ... ...
8. Conclusion
In this article, we’ve discussed how to list only directories for any path in Linux. We’ve accomplished that using the commands ls, dir, find, tree, and echo.