Live Chat!

How to list just directories ( the correct way)

* * * * * 2 votos

September 21st, 2007 mysurface Posted in Common, ls | Hits: 16357 |

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

5 Responses to “How to list just directories ( the correct way)”

  1. [...] The statement above is WRONG, check out the simple way to list just directories with ls -d [...]

  2. This is useful!

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

  4. [...] 这篇文章里面还提到了更多关于ls的技巧,特别是后面的用户留言里面有人发了一个用法的总结上面,有兴趣的可以跟过去看看。 [...]

  5. Is there a way to list just the files?

Leave a Reply