O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Listing only directories – alternative methods

Though listing only directories seems to be a simple task, many would not be able to do it. I have seen this often, even when asked to people who are good at shell scripting. This recipe is worth knowing since it introduces multiple ways of listing only directories with various tricky techniques.

Getting ready

There are multiple ways of listing directories only. When you ask people about these techniques, the first answer that they would probably give is dir. But, it is wrong. The dir command is just another command like ls with fewer options than ls. Let's see how to list directories.

How to do it...

There are four ways in which directories in the current path can be displayed. They are:

  • $ ls -d */

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required