How to List only Directories in Linux ls Command?
The ls command which is used to list files and directories on Linux does not have a command option that lists only directories (Folder).
However, we can Use the ls -l command in combination with the grep command to list only directories.
ls -l | grep "^d"
The preceding command will list directories under the current working directory. If you want to include hidden folders, use the ls -la with grep command, as shown in the following example:
ls -la | grep "^d"
This command is long and it’s difficult to type every time you want to see a list of directories. It is better to create an alias (command shortcut) for the command that we just executed.
alias lsd="ls -la | grep '^d'"
Add the alias to the ~/.bashrc file to make it permanent.
How It Works
In the ls -l output, the first letter of the first column identifies the file type. The first character is a d if the file type is a directory.
By piping the ls -l output to grep, we look for files that begin with the d character.
The caret (^) symbol is a regular expression that finds every line with the d character at the beginning of the string.
We also created an alias called "lsd", so now we can get a list of directories in the current working directory without having to type the long command.