[unix] Listing only directories in UNIX

I want to list only the directories in specified path (ls doesn't have such option). Also, can this be done with a single line command?

This question is related to unix shell

The answer is


To list only directories in a specified path,just type ls -l | grep drw


du -d1 is perhaps the shortest option. (As long as you don't need to pipe the input to another command.)


ls -l | grep '^d'

You can make an alias and put it into the profile file

alias ld="ls -l| grep '^d'"

You can use the tree command with its d switch to accomplish this.

% tree -d tstdir
tstdir
|-- d1
|   `-- d11
|       `-- d111
`-- d2
    `-- d21
        `-- d211

6 directories

see man tree for more info.


If I have this directory:

ls -l

lrwxrwxrwx  1 nagios nagios     11 août   2 18:46 conf_nagios -> /etc/icinga
-rw-------  1 nagios nagios 724930 août  15 21:00 dead.letter
-rw-r--r--  1 nagios nagios  12312 août  23 00:13 icinga.log
-rw-r--r--  1 nagios nagios   8323 août  23 00:12 icinga.log.gz
drwxr-xr-x  2 nagios nagios   4096 août  23 16:36 tmp

To get all directories, use -L to resolve links:

ls -lL | grep '^d'

drwxr-xr-x 5 nagios nagios   4096 août  15 21:22 conf_nagios
drwxr-xr-x 2 nagios nagios   4096 août  23 16:41 tmp

Without -L:

ls -l | grep '^d'

drwxr-xr-x 2 nagios nagios   4096 août  23 16:41 tmp

conf_nagios directory is missing.


Here's another solution that shows linked directories. I slightly prefer it because it's a subset of the "normal" ls -l output:

ls -1d */ | rev | cut -c2- | rev | xargs ls -ld --color=always

The answer will depend on your shell.

In zsh, for example, you can do the following:

echo *(/)

And all directories within the current working directory will be displayed.

See man zshexpn for more information.

An alternative approach would be to use find(1), which should work on most Unix flavours:

find . -maxdepth 1 -type d -print  

find(1) has many uses, so I'd definitely recommend man find.


### If you need full path of dir and list selective dir with "name" of dir(or dir_prefix*):
find $(pwd) -maxdepth 1 -type d -name "SL*"

In bash:

ls -d */

Will list all directories

ls -ld */

will list all directories in long form

ls -ld */ .*/

will list all directories, including hidden directories, in long form.


I have recently switched to zsh (MacOS Catalina), and found that:

ls -ld */ .*/

no longer works if the current directory contains no hidden directories.

zsh: no matches found: .*/

It will print the above error, but also will fail to print any directories.

ls -ld *(/) .*(/)

Also fails in the same way.

So far I have found that this:

ls -ld */;ls -ld .*/

is a decent workaround. The ; is a command separator. But it means that if there are no hidden directories, it will list directories, and still print the error for no hidden directories:

foo
bar
zsh: no matches found: .*/

ls is the shell command for list contents of current directory
-l is the flag to specify that you want to list in Longford (one item per line + a bunch of other cool information)
-d is the flag to list all directories "as files" and not recursively
*/ is the argument 'list all files ending in a slash'
* is a simple regex command for "anything", so */ is asking the shell to list "anything ending in '/'"

See man ls for more information.


I put this:

alias lad="ls -ld */;ls -ld .*/"

in my .zshrc, and it seems to work fine.

NOTE: I've also discovered that

ls -ld .*/ 2> /dev/null

doesn't work, as it still prints sterr to the terminal. I'll update my answer if/when I find a solution.


use this to get a list of directory

ls -d */ | sed -e "s/\///g"

In order to list the directories in current working directory ls -d */ can be used. And If you need to list the hidden directories use this command ls -d .*/


Try this ls -d */ to list directories within the current directory


Try this:

find . -maxdepth 1 -type d

find . -maxdepth 1 -type d -name [^\.]\* | sed 's:^\./::'

find specifiedpath -type d

If you don't want to recurse in subdirectories, you can do this instead:

find specifiedpath -type d -mindepth 1 -maxdepth 1

Note that "dot" directories (whose name start with .) will be listed too; but not the special directories . nor ... If you don't want "dot" directories, you can just grep them out:

find specifiedpath -type d -mindepth 1 -maxdepth 1 | grep -v '^\.'


Long listing of directories

  • ls -l | grep '^d'
  • ls -l | grep "^d"

Listing directories
- ls -d */


The following

find * -maxdepth 0 -type d

basically filters the expansion of '*', i.e. all entries in the current dir, by the -type d condition.

Advantage is that, output is same as ls -1 *, but only with directories and entries do not start with a dot


This has been working for me:

`ls -F | grep /`

(But, I am switching to echo */ as mentioned by @nos)


Since there are dozens of ways to do it, here is another one:

tree -d -L 1 -i --noreport
  • -d: directories
  • -L: depth of the tree (hence 1, our working directory)
  • -i: no indentation, print names only
  • --noreport: do not report information at the end of the tree listing

You can use ls -d */ or tree -d

Another solution would be globbing but this depends on the shell you are using and if globbing for directories is supported.

For example ZSH:

zsh # ls *(/)