About Linux FAQ

Browse More FAQs:

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

Posted by Vivek Gite [Last updated: September 21, 2007]

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
....
.....
.

E-mail this to a friend      Printable version

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Eric Says:

    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 Says:

    find . -type d -maxdepth 1

  3. mysurface Says:

    ls -d */

    or for hidden directories
    ls -d .*/

  4. vivek Says:

    Thanks for contributing all other tips :)

  5. Vinayak Says:

    Thanks a lot for nice recipe… :-)

  6. Michel Roig Says:

    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 Says:

    For some reasons on FAT fs

    ls -d */

    is not working. But this works on any filesystem:

    ls -d ./*/

  8. Suresh M Says:

    echo */

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.