Listing only directories in UNIX
UnixShellUnix Problem Overview
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?
Unix Solutions
Solution 1 - Unix
Try this ls -d */
to list directories within the current directory
Solution 2 - Unix
Try this:
find . -maxdepth 1 -type d
Solution 3 - Unix
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
Solution 4 - Unix
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 *(/)
Solution 5 - Unix
ls -l | grep '^d'
You can make an alias and put it into the profile file
alias ld="ls -l| grep '^d'"
Solution 6 - Unix
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
Solution 7 - Unix
find . -maxdepth 1 -type d -name [^\.]\* | sed 's:^\./::'
Solution 8 - Unix
use this to get a list of directory
ls -d */ | sed -e "s/\///g"
Solution 9 - Unix
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
.
Solution 10 - Unix
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 .*/
Solution 11 - Unix
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 '^\.'
Solution 12 - Unix
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.
Solution 13 - Unix
Long listing of directories
ls -l | grep '^d'
Listing directories
ls -d */
Solution 14 - Unix
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.
Solution 15 - Unix
### 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*"
Solution 16 - Unix
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.
Solution 17 - Unix
This has been working for me:
ls -F | grep /
(But, I am switching to echo */
as mentioned by @nos)
Solution 18 - Unix
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
Solution 19 - Unix
This is the answer most people will want.
ls -l | grep -E '^d' | awk '{print $9}'
The directory names, and nothing but the directory names.
Solution 20 - Unix
I find there are many good answers listed before me. But I would like to add a command which we already use it several time, and so very easy to list all the directories with less effort:
> cd
(Note: After cd give a space) and press tab twice, it will list only all the directories in current working directory. Hope this is easy to use. Please let me know if there is any problem with this. Thanks.
Solution 21 - Unix
du -d1
is perhaps the shortest option. (As long as you don't need to pipe the input to another command.)
Solution 22 - Unix
To list only directories in a specified path,just type
ls -l | grep drw