Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing only directories in UNIX

Tags:

shell

unix

People also ask

How do I list only directories in Unix?

Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.

How do I get a list of directories in Linux?

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

How do I view only directories in ls?

ls is a Linux and Unix command that allows you to list all files and directories in a directory. You can also use ls -d to view only directories and use ls -f to view only files as shown above.


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


Try this:

find . -maxdepth 1 -type 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


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

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

ls -l | grep '^d'

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

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

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