Advertisement
Advertisement


Listing only directories in UNIX


Question

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?

2014/08/01
1
221
8/1/2014 2:36:15 AM


Try this:

find . -maxdepth 1 -type d
2010/09/08

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

2017/08/06

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 *(/)
2010/09/08

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
2014/05/23

ls -l | grep '^d'

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

alias ld="ls -l| grep '^d'"
2015/01/24

Source: https://stackoverflow.com/questions/3667329
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]