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 .... ..... .
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 9-21-07

{ 17 comments… read them below or add one }
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
find . -type d -maxdepth 1
ls -d */
or for hidden directories
ls -d .*/
Thanks for contributing all other tips :)
Thanks a lot for nice recipe… :-)
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 {} \;
For some reasons on FAT fs
ls -d */
is not working. But this works on any filesystem:
ls -d ./*/
echo */
mysurface — you are the man!
(folders)
ls -F | grep /
(files)
ls -F | grep -v /
Lists all the subdirectories in the current directory..
ls -R | grep ./
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”
..
.
~
. Current directory
.. Parent directory
~ Your home directory
Can you just list the directories name, without the permission, datetime, and etc … ?
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 :)
oops! didnt read the other comments. Already many nice solutions have been posted :P
i used to the $ du command to retrieve all directory names.
$ du