Bash – correct way to list the subdirectories of the current directory

bashcommand linedirectoryls

I can find the subdirectories of a directory by

ls -d -- */

or

ls -l | grep "^d"

but both of these seem indirect, and I would imagine that there would be a standard way to find just the directories. Is there a right way to do this? And if not, will either of these lead to undesirable behavior on edge cases? (Symbolic links, hidden directories, etc.)

Best Answer

The answer will depend more on what you intend to do with the output than on what you are looking for. If you just want to see a list for visual reference at the terminal, your first solution is actually pretty nice. If you want to process the output you should consider using another method.

One of the most robust ways to get a list to feed into another program is to use find.

find -maxdepth 1 -type d

The reason this is good for feeds is that find can output the data separated by nulls using -print0 or properly escape strings as arguments to another programs using -exec. For reference on why this is better than parsing the output of ls, see ParsingLS on Greg's Wiki.

Related Question