Skip to content

How to list directories with ls?

It just occurred to me that I didn’t know how to list only directories, so here it is:

ls -d */

How does this work?

  1. li -d lists only the directory entires, so instead of listing directory and also whats in it, it lists only the directory name
  2. */ is expended by the shell (bash in my case) into pwd/*/ (and this is why ls by itself just works, because pwd is appended automatically)
  3. So basically pwd/*/ means match everything with / at the end, which are directories, and -d says do not list whats inside. So all that are left are the directory names

Posted in Linux. Tagged with , .