Skip to content
shellhacks.dev
Command Line

List Only Directories on Linux with ls -d */

List only directories with ls -d */, which prints each name with a trailing slash. Symlinks to directories are included and hidden ones are not.

1 min read
Summarize in AI

A directory is full of files and you want to see just the subdirectories.

Shell
mkdir a "b c" .hidden
touch file
ln -s a link
ls -d */
echo ---
find . -mindepth 1 -maxdepth 1 -type d | sort
Text
a/
b c/
link/
---
./.hidden
./a
./b c

The shell expands */ to every name that is a directory, or a symlink to one, and -d makes ls list those names instead of their contents. Without it, ls would open each directory and list what is inside. The pattern skips dotfiles by default, and find -type d does not follow symlinks, so the two commands disagree on link and .hidden as the output shows. The options are in the ls manual.

When there are no subdirectories the glob stays as the literal text */ and ls reports that no such file exists. Check with compgen -G '*/' in Bash or turn on nullglob. A very long list can hit the argument limit, where find has no such limit.

Share

More snippets

Command Line

Avoid Parsing ls Output in Shell Scripts

Do not loop over the output of ls in a script. File names with spaces or a newline split into pieces, while a glob such as ./* hands over each name whole.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.