How to list just directories ( the correct way)
September 21st, 2007 mysurface Posted in Common, ls | Hits: 76486 | 18 Comments »
I have wrote a post stated that there are no direct ways to list just directories, and wrote a bash scripts using find to list the directories. I had made a WRONG statement. We can list just directories with ls -d, thanks to Ntropia who leaves me a comment.
Before that I had tried hard to use ls -d to list just directories but failed, therefore I wrote a bash script uses find to do that. It seems Ntropia provided a better solution and it is straight to the point, so let just treat the previous post as examples of find command.
The simplest way of list just directories
ls -d */
You can list the directories start with letter b
ls -d b*/
Further more list the subdirectories of the directories start with letter b
ls -d b*/*/
The outcome will be look like this
backup/10-5-2007/ backup/lunatic/ bin/gdc/
backup/ccbe/ backup/wplbe/ bt/çŽ‹åŠ›å® - 改å˜è‡ªå·±/
backup/full/ bin/ffmpeg-0.4.9-p20051216/
Bare in mind the command lines above do not list out the hidden directories, to list hidden directories
ls -d .*/
Yes, you can also list with details, and with nice colors.
ls -d .*/ -l
Manual of ls should at lease gives a line of example on how to list just directories, ls capable of doing that but it seems to like easter egg for me.
Updates
In fact, I had done the post of list just directories long time ago, :P







September 21st, 2007 at 12:26 am
[...] The statement above is WRONG, check out the simple way to list just directories with ls -d [...]
September 21st, 2007 at 8:37 am
This is useful!
September 22nd, 2007 at 2:42 am
ls is a very powerful command, but in its simplest forms it really seems counterintuitive. For example:
ls
As you would expect it, lists the current directory contents.
ls *
Lists the contents of all the subdirectories of the current directory (?)
ls -a
The same as ls, but including hidden contents.
ls -d
ls -ad
Just a dot! Hardly useful.
ls -d *
ls -ad *
The man page says “-d list directory entries instead of contents [...]“. But this just gives the same as ls. The -a switch *does not* include hidden contents.
ls -d */
ls -ad */
Just the directories, but not the hidden ones. The man and info pages don’t mention the slash trick, so I don’t know how is one supposed to learn it.
I could go on and on. One of the reasons I use mc for file management is the complexity of ls and find. Maybe the dir command should have been for a simpler, user-friendly version of ls instead of an alias.
November 4th, 2007 at 4:07 am
[...] è¿™ç¯‡æ–‡ç« é‡Œé¢è¿˜æåˆ°äº†æ›´å¤šå…³äºŽls的技巧,特别是åŽé¢çš„ç”¨æˆ·ç•™è¨€é‡Œé¢æœ‰äººå‘了一个用法的总结上é¢ï¼Œæœ‰å…´è¶£çš„å¯ä»¥è·Ÿè¿‡åŽ»çœ‹çœ‹ã€‚ [...]
February 25th, 2008 at 2:44 am
Is there a way to list just the files?
December 2nd, 2008 at 6:14 pm
To list files, we could list by just the file extensions.
April 1st, 2009 at 2:21 pm
[...] http://linux.byexamples.com/archives/314/how-to-list-just-directories-the-correct-way/ [...]
May 30th, 2009 at 8:00 am
find * -prune -type d
This list directories whithout the ending slash (the correct way)
May 30th, 2009 at 8:01 am
find * -prune -type f
And just for the files… ;)
July 31st, 2009 at 2:04 pm
walk directory tree in a script:
base=$(pwd)
for folder in $(find * -type d)
do
cd $folder
# insert your code here
cd $base
done
December 30th, 2010 at 10:54 am
SO good the job your are. Thank for the tips and tricks
July 6th, 2011 at 5:54 am
Another way to list only directories or only files is to use ls and egrep together
to list only directories
ls | egrep ‘^d’
to list only files
ls | egrep -v ‘^d’
note the -v inverts the selection, so the opposite of the first command is returned.
September 11th, 2011 at 3:58 pm
# Recursively list directories.
shopt -s globstar
ls -d **/
January 20th, 2012 at 1:12 am
I agree with Salazar. The ls man page fails on this matter.
January 31st, 2012 at 9:37 pm
# list only directory names in “location”:
find “$location” -mindepth 1 -maxdepth 1 -type d -printf ‘%f’
May 10th, 2012 at 12:15 pm
find “$address” -maxdepth 1 -type d -printf ‘%f\n’ | tail -n+2 | sort
The find command’s -type d option lists the directories, the -printf ‘%f\n’ restricts output to just the filenames minus their paths and puts each entry on its own line. Piping through tail -n+2 gets rid of the first line which is always the name of the directory being examined. Piping through sort organises them sensibly.
Hard to believe such a simple thing is missing from the basic linux toolset.
There is the GNU ‘dir’ command. Using its -d option lists just directories, however there is no simple way to do it without their paths, other than piping through sed:
dir -d “$address/*” | sed ‘s/.*\/\(.*\)/\1/g’
Unfortunately the ‘/*’ is needed at the end of the address being queried.
May 10th, 2012 at 6:00 pm
Oops… the ‘dir’ command doesn’t work quite as expected. It needs one final ‘/’ on the command so that it matches directories only, otherwise it prints out all files, directories or not, regardless of the ‘-d’ switch. I should have written:
dir -d “$address/*/”
Also, the sed command I gave won’t work either because the output of the dir listing ends with slashes. If you want just the directory names without the final slashes then you’d have to lop the trailing slash with perhaps sed:
sed ‘s/\(.*\).$/\1/g’
then get rid of the paths, perhaps with sed again:
sed ‘s/.*\/\(.*\)/\1/g’
But this is starting to become quite ridiculous:
dir -d “$address/*/” | sed ‘s/\(.*\).$/\1/g’ | sed ‘s/.*\/\(.*\)/\1/g’
Wouldn’t it be so much easier if linux had a simple option for ls to list directories?
My old Amiga can do it easily. Even crappy old MSDOS can. I’m amazed that linux can’t.
December 15th, 2012 at 10:46 am
Thanks for the great information…… Chris