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 /etcOutput:
$ ldir
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:
- How do I check and repair MS-DOS file systems under Linux?
- Linux/UNIX: Rules for naming file and directory names
- What you need to back up on Linux to recover
- Restore Selected Files From Secondary Backup Hard Disk
- UNIX Find A File Command
Discussion on This FAQ
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!
Tags: directory_names, find_command, grep_command, Linux, list_directory, ls_command, UNIX




April 26th, 2007 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
September 18th, 2007 at 7:39 am
find . -type d -maxdepth 1
September 21st, 2007 at 12:15 am
ls -d */
or for hidden directories
ls -d .*/
September 21st, 2007 at 12:52 pm
Thanks for contributing all other tips
September 26th, 2007 at 5:56 am
Thanks a lot for nice recipe…
October 1st, 2007 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 {} \;
November 18th, 2007 at 3:12 pm
For some reasons on FAT fs
ls -d */
is not working. But this works on any filesystem:
ls -d ./*/
January 4th, 2008 at 10:52 am
echo */