How to List Only Directories in Linux

list only directories in linux

The ls command in Linux is used to list the files and directories in a directory. But if you wish to list directories only using ls command, what are the options?

We learn how to use command chaining (using pipes) to see what directories are present in a given directory.

In this tutorial, I will show you a number of ways to list directories only in Linux.

Listing directories using Wildcards

The simplest method is using wildcards. All the directories end in forward slash.

$ ls -d */
Desktop/ Downloads/ looped/ Music/ orb.db/ Pictures/ Templates/ Videos/ Virus/
Documents/ jvm/ mounts/ netbeans-6.9/ PacketTracer5/ Public/ Ubuntu One/ VirtualBox VMs/

For the long listing, just add -l option.

$ ls -ld */
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop/
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents/
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads/
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm/
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped/
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music/
drwxr-xr-x 12 raghu raghu 4096 2012-01-24 19:29 netbeans-6.9/
drwxr-xr-x 3 raghu raghu 4096 2012-04-17 12:23 orb.db/
drwxr-xr-x 4 raghu raghu 4096 2011-11-16 13:03 PacketTracer5/
drwxr-xr-x 5 raghu raghu 4096 2012-07-16 11:41 Pictures/

Using -F option and grep

The -F options appends a trailing forward slash. So we can grep the directories only by 'grep' ing lines ending with a forward slash (/).

$ ls -lF | grep \/$
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop/
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents/
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads/
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm/
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped/
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts/
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music/
drwxr-xr-x 12 raghu raghu 4096 2012-01-24 19:29 netbeans-6.9/
drwxr-xr-x 3 raghu raghu 4096 2012-04-17 12:23 orb.db/
drwxr-xr-x 4 raghu raghu 4096 2011-11-16 13:03 PacketTracer5/
drwxr-xr-x 5 raghu raghu 4096 2012-07-16 11:41 Pictures/

or for just the directory names, without -l option,

$ ls -F | grep \/$
Desktop/
Documents/
Downloads/
jvm/
looped/
mounts/
Music/
netbeans-6.9/
orb.db/
PacketTracer5/
Pictures/

Using -l option and grep

In the long listing of ls i.e. ls -l, we can ‘grep’ the lines starting with d.

$ ls -l | grep ^d
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads
drwxr-xr-x 2 raghu raghu 4096 2012-03-27 10:48 jvm
drwxr-xr-x 2 raghu raghu 4096 2011-10-29 13:02 looped
drwxr-xr-x 2 root root 4096 2011-11-15 01:23 mounts
drwxr-xr-x 2 raghu raghu 4096 2011-10-26 16:03 Music

We can extract just the file names by printing only the last columns.

$ ls -l | grep ^d | awk '{print $8,$9}'
Desktop
Documents
Downloads
jvm
looped
mounts
Music

Using echo command

We can use echo command to list the entries trailing with forward slash (/).

$ echo */
Desktop/ Documents/ Downloads/ jvm/ looped/ mounts/ Music/ netbeans-6.9/ orb.db/ PacketTracer5/ Pictures/

Using printf

Similarly, printf can be used to highlight strings ending with forward slash (/).

$ printf '%s\n' */
Desktop/
Documents/
Downloads/
jvm/
looped/
mounts/

Using find command

We can always find files based on their file types using find command:

$ find . -maxdepth 1 -type d
.
./.netbeans-registration
./.gvfs
./.shotwell
./.gegl-0.0
./.cache
./.ssh
./VirtualBox VMs
./.mission-control
./.gnome2
./.jedit
./.config
./mounts

The maxdepth option in the above command specifies that the search is to be performed in specified directory only. Otherwise, find command will find the directories recursively, by traversing each directory and their subdirectories. Also, in this command, the hidden directories are also shown. In all above methods that use ls command, the same can be achieved through -a option. For example,

$ ls -laF | grep \/$
drwxr-xr-x 60 raghu raghu 4096 2012-09-28 20:18 ./
drwxr-xr-x. 6 root root 4096 2012-06-25 01:08 ../
drwx------ 3 raghu raghu 4096 2011-10-29 14:04 .adobe/
drwx------ 28 raghu raghu 4096 2012-09-28 20:07 .cache/
drwx------ 3 raghu raghu 4096 2011-10-26 16:06 .compiz/
drwxr-xr-x 26 raghu raghu 4096 2012-09-27 00:20 .config/
drwx------ 3 raghu raghu 4096 2011-10-26 16:03 .dbus/
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop/
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents/
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads/
drwxr-xr-x 7 raghu raghu 4096 2012-09-15 23:21 .drpython/
drwxr-xr-x 2 raghu raghu 4096 2012-09-10 14:28 .fontconfig/
drwx------ 5 raghu raghu 4096 2012-09-28 20:07 .gconf/
drwx------ 2 raghu raghu 4096 2012-09-28 23:10 .gconfd/
drwx------ 4 raghu raghu 4096 2011-11-26 20:23 .gegl-0.0/
drwxr-xr-x 22 raghu raghu 4096 2012-09-27 01:44 .gimp-2.6/
drwx------ 11 raghu raghu 4096 2012-09-22 05:23 .gnome2/

or

$ ls -la | grep ^d
drwxr-xr-x 60 raghu raghu 4096 2012-09-28 20:18 .
drwxr-xr-x. 6 root root 4096 2012-06-25 01:08 ..
drwx------ 3 raghu raghu 4096 2011-10-29 14:04 .adobe
drwx------ 28 raghu raghu 4096 2012-09-28 20:07 .cache
drwx------ 3 raghu raghu 4096 2011-10-26 16:06 .compiz
drwxr-xr-x 26 raghu raghu 4096 2012-09-27 00:20 .config
drwx------ 3 raghu raghu 4096 2011-10-26 16:03 .dbus
drwxr-xr-x 4 raghu raghu 4096 2012-09-27 23:50 Desktop
drwxr-xr-x 13 raghu raghu 4096 2012-09-16 13:22 Documents
drwxr-xr-x 5 raghu raghu 4096 2012-09-28 15:01 Downloads
drwxr-xr-x 7 raghu raghu 4096 2012-09-15 23:21 .drpython

Thanks for reading this article. Let me know your thoughts.

Read Also:

Raghu 7:35 pm

Comments

Your email address will not be published. Required fields are marked *