Linux: terminal: listing subdirectories (directories) only

The tutor shares a nice tip that he finds very helpful:  listing the subdirectories only with the ls command.

I write a lot of little programs to test built-in functions or show examples of their use.  Of course, as they accumulate, organizing them becomes important.

Before reading further, an important relationship to keep in mind is that a directory is to the terminal what a folder is to the desktop. A subdirectory, then, is like a folder within a folder. I use both file environments, so I use the terms directory and folder interchangeably.

Very often, I’ll store a day’s work in a folder labeled that date, within another folder for the project to which it belongs or what have you.  Returning weeks later, I need to know, from an upper directory, where the folder (aka subdirectory) of a particular date might be.

While there are several ways to find out, I like ls because it’s quick and minimal -especially if you know how to tailor it. The good people at stackoverflow.com offer the variation

ls -d */

as the way to return the subdirectories only.

But what if you want (like me) to know how deep the structure goes?

You can check for second-level subdirectories thus:

ls -d */*/

or third-level thus:

ls -d */*/*/

You can also do simultaneous checks:

ls -d */ */*/

reveals the subdirectories in the current one, as well as the ones they contain.

There might be better ways to search around, but for my purposes these variations of ls work great.

HTH:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply