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
Key Facts
- The `ls -d */` command lists only directories in the current location.
- The `ls -F | grep '/$'` command filters `ls` output to show directories.
- The `find . -maxdepth 1 -type d` command is a robust alternative.
- The `-maxdepth 1` option in `find` prevents recursion into subdirectories.
- The `-type d` option in `find` specifically targets directory entries.
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.mdRunning `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:
- `ls -F`: Lists the contents of the current directory and appends indicators (e.g., `/` for directories, `*` for executables, `@` for symbolic links).
- `|`: This is the pipe operator, which sends the output of the `ls -F` command as input to the `grep` command.
- `grep '/$'`: This searches for lines that end (`$`) with a forward slash (`/`).
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 dLet's break down this command:
- `find .`: Starts the search in the current directory (`.`).
- `-maxdepth 1`: This crucial option limits the search depth to only the current directory. Without it, `find` would recursively list all directories within subdirectories as well.
- `-type d`: This option tells `find` to search specifically for entries of type directory (`d`).
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 dHere, `-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:
- For a quick listing of immediate subdirectories in the current location, `ls -d */` is often the simplest.
- If you need to include hidden directories or prefer a filtering approach, `ls -F | grep '/$'` (potentially with `-a`) is a good option.
- For more control, scripting, or when you need to be absolutely explicit about finding only directories (and potentially control recursion depth), the `find` command (`find . -maxdepth 1 -type d` or `find . -mindepth 1 -maxdepth 1 -type d`) is the most powerful and recommended choice.
Understanding these variations allows you to efficiently manage and query your file system from the command line.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- ls(1) - Linux man pageGPL-3.0-or-later
- find(1) - Linux man pageGPL-3.0-or-later
- Bash Reference Manual - WildcardsCC-BY-ND-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.