How to ls only directories

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To list only directories using the `ls` command in Linux/macOS, you can use the `-d` option combined with a wildcard pattern like `*/` or pipe the output to `grep` to filter for directory entries. Alternatively, the `find` command offers a more direct way to achieve this.

Key Facts

Overview

The `ls` command is a fundamental utility in Unix-like operating systems (Linux, macOS) used to list directory contents. While its primary function is to display files and directories, users often need to isolate and view only the directory entries. This can be useful for various scripting tasks, system administration, or simply for navigating file systems more efficiently. Fortunately, there are several straightforward methods to achieve this using `ls` and other related command-line tools.

Listing Only Directories with `ls`

The `ls` command itself provides options that can help filter the output. One of the most direct ways to list only directories is by using the `-d` option in conjunction with a wildcard pattern.

Using `ls -d */`

The asterisk (`*`) is a wildcard character that matches any sequence of characters. When followed by a forward slash (`/`), the pattern `*/` specifically targets entries that are directories. The `-d` option tells `ls` to list directories themselves, rather than their contents. If you run `ls -d */` in a directory, it will output the names of all subdirectories, each followed by a slash.

For example, if you have a directory structure like this:

my_project/src/docs/main.pyREADME.md

Running `ls -d */` in `my_project/` would produce:

docs/ src/

This is a concise and often preferred method for quickly seeing only the immediate subdirectories.

Using `ls -F` and `grep`

Another approach involves using the `-F` option with `ls`, which appends an indicator character to entries to denote their type. Directories are typically indicated by a trailing forward slash (`/`). You can then pipe this output to the `grep` command to filter for lines ending with a slash.

The command would look like this:

ls -F | grep '/$'

Here:

This method is effective but slightly more verbose than `ls -d */`. It also lists hidden directories (those starting with a dot) if you combine it with `ls -aF | grep '/$'`.

Using the `find` Command

While `ls` is the primary tool for listing directory contents, the `find` command is a more powerful and flexible utility for searching for files and directories based on various criteria. It offers a very direct way to list only directories.

Using `find . -maxdepth 1 -type d`

This command is arguably the most robust and explicit method:

find . -maxdepth 1 -type d

Let's break down this command:

The output of this command will be a list of directories, including the current directory itself (`.`). If you wish to exclude the current directory from the results, you can add another condition or use a different starting point, though for simply listing subdirectories, `find . -mindepth 1 -maxdepth 1 -type d` is more precise.

Excluding the Current Directory with `find`

To list only the subdirectories and exclude the current directory (`.`), you can use the `-mindepth` option:

find . -mindepth 1 -maxdepth 1 -type d

Here, `-mindepth 1` ensures that `find` only considers entries that are at least one level deep, effectively skipping the starting directory (`.`).

Choosing the Right Method

The best method depends on your specific needs:

Understanding these variations allows you to efficiently manage and query your file system from the command line.

Sources

  1. ls(1) - Linux man pageGPL-3.0-or-later
  2. find(1) - Linux man pageGPL-3.0-or-later
  3. Bash Reference Manual - WildcardsCC-BY-ND-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.