How to: Linux or UNIX List just directories or directory names

Q. How do I list just directory names under Linux?

A. Under Linux or UNIX use ls command to list files and directories. However ls does not have an option to list only directories. You can use combination of ls and grep to list directory names only.

Display or list all directories

Type the following command:
$ ls -l | egrep `^d'

Display or list only files

Type the following command:
$ ls -l | egrep -v `^d'

grep command used to searches input. It will filter out directories name by matching first character d. To reverse effect (just to display files) you need to pass -v option. It invert the sense of matching, to select non-matching lines.

Task: Create aliases to save time

You can create two aliases as follows to list only directories and files.
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"

Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile

Append two lines:
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"

Save and close the file.

Now just type lf - to list files and ldir - to list directories only:
$ cd /etc
$ lf

Output:

-rw-r--r--   1 root root      2149 2006-09-04 23:25 adduser.conf
-rw-r--r--   1 root root        44 2006-09-29 05:11 adjtime
-rw-r--r--   1 root root       197 2006-09-04 23:48 aliases
-rw-------   1 root root       144 2002-01-18 13:43 at.deny
-rw-r--r--   1 root root       162 2006-09-22 23:24 aumixrc
-rw-r--r--   1 root root        28 2006-09-22 23:24 aumixrc1
....
..
....

List directory names only:
$ cd /etc
$ ldir
Output:

drwxr-xr-x   4 root root      4096 2006-09-22 16:41 alsa
drwxr-xr-x   2 root root      4096 2006-09-20 20:59 alternatives
drwxr-xr-x   6 root root      4096 2006-09-22 16:41 apm
drwxr-xr-x   3 root root      4096 2006-09-07 02:51 apt
drwxr-xr-x   2 root root      4096 2006-09-08 01:46 bash_completion.d
....
.....
.
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 17 comments… read them below or add one }

1 Eric 04.26.07 at 1:07 pm

To get just file names without the long list data:

ls -l | grep ‘^d’ | awk ‘{ print $9 }’

Or

for foo in *; do if [ -d $foo ]; then print -n ” $foo”; else false; fi; done

2 Lingerance 09.18.07 at 7:39 am

find . -type d -maxdepth 1

3 mysurface 09.21.07 at 12:15 am

ls -d */

or for hidden directories
ls -d .*/

4 vivek 09.21.07 at 12:52 pm

Thanks for contributing all other tips :)

5 Vinayak 09.26.07 at 5:56 am

Thanks a lot for nice recipe… :-)

6 Michel Roig 10.01.07 at 2:48 pm

To get just the subdirectories names

find . -type d -maxdepth 1 -exec basename {} \;

However, in this case you also get the directory itself! To avoid this, add mindepth 1:

find . -type d -maxdepth 1 -mindepth 1 -exec basename {} \;

7 Eugenio 11.18.07 at 3:12 pm

For some reasons on FAT fs

ls -d */

is not working. But this works on any filesystem:

ls -d ./*/

8 Suresh M 01.04.08 at 10:52 am

echo */

9 Al 01.11.09 at 5:19 pm

mysurface — you are the man!

10 biodafes 01.21.09 at 1:18 am

(folders)
ls -F | grep /
(files)
ls -F | grep -v /

11 Mahantesh Biradar 01.30.09 at 5:13 am

Lists all the subdirectories in the current directory..

ls -R | grep ./

12 Gareth Reeman 05.01.09 at 6:19 pm

can some1 please help me i need to know what the significant effects are of the following characters in unix filenames. 2 dots / 1 dot / tilde or “squiggle”
..
.
~

13 Vivek Gite 05.01.09 at 8:47 pm

. Current directory
.. Parent directory
~ Your home directory

14 Dennis Quek 05.05.09 at 7:35 am

Can you just list the directories name, without the permission, datetime, and etc … ?

15 kopla 05.08.09 at 10:22 am

Thanx, nice post. I was just thinking of doing it with awk but couldnt get the wildcard working. Now I am one step ahead in my shell knowledge ^^
To Dennis Quek above :
A rather dirty solution but does the job.

ls -l | egrep ‘^d’ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘| awk ‘$1=” “‘| awk ‘$1=” “‘

(forgive me if seeing this code gives someone a heart attack, all i can say is that I’m still learning :P )
I would love if someone can give my code a neater look :)

16 kopla 05.08.09 at 10:34 am

oops! didnt read the other comments. Already many nice solutions have been posted :P

17 jagan 06.30.09 at 12:29 pm

i used to the $ du command to retrieve all directory names.
$ du

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , ,

Previous post: Linux: How can I find a file on my system?

Next post: How to include files with PHP