How To List Only Directories In Linux – Exclude Regular File Output

Passing the `-d` option to `ls` and using a pattern like `*/` isolates directory entries from other file types. If you’ve ever found yourself staring at a cluttered terminal output, wondering how to list only directories in Linux, you’re not alone. This is a common task for system administrators and developers who need to quickly navigate file systems without the noise of regular files.

In this guide, I’ll show you multiple ways to filter out everything but directories. You’ll learn commands that work in bash, zsh, and other common shells. By the end, you’ll be able to list directories like a pro.

How To List Only Directories In Linux

The most direct method uses the `ls` command with a specific pattern. Open your terminal and type:

ls -d */

This command tells `ls` to list directory entries (`-d`) that match the pattern `*/`. The slash at the end is a wildcard that matches only directories. The output shows just the folder names in the current directory.

But there are other ways too. Let’s explore each method step by step.

Using Ls With The -D Flag

The `-d` flag prevents `ls` from listing the contents of directories. Without it, `ls */` would show files inside each directory. With `-d`, you only see the directory names themselves.

Example output:

Documents/ Downloads/ Music/ Pictures/

This works in any POSIX-compliant shell. It’s the simplest solution for most users.

Using Find Command

The `find` command is more powerful. It can search recursively and apply complex filters. To list only directories in the current directory (non-recursive):

find . -maxdepth 1 -type d

This finds everything in the current directory (`.`) with a maximum depth of 1, and only entries of type directory (`-type d`). The output includes the leading `./` prefix.

To list directories recursively:

find . -type d

This shows all directories under the current location. You can also exclude hidden directories (those starting with a dot) using:

find . -type d -not -name '.*'

Using Grep With Ls

If you prefer piping, combine `ls` with `grep`. The `ls -l` output shows directories with a `d` at the start of the line. You can filter for that:

ls -l | grep '^d'

This shows detailed directory listings. To get just names, add `awk`:

ls -l | grep '^d' | awk '{print $9}'

This extracts the ninth field (the filename) from each line. It’s a bit hacky but works in a pinch.

Using Tree Command

The `tree` command visualizes directory structures. To list only directories:

tree -d

This shows a tree of directories without files. You can limit depth with `-L`:

tree -d -L 1

This shows only the top-level directories. Install `tree` if it’s not already on your system:

sudo apt install tree   # Debian/Ubuntu

Using Bash Globbing

Bash itself can expand directory patterns. The `*/` pattern works in many contexts:

echo */

This prints all directories in the current folder, separated by spaces. It’s quick for scripting.

For hidden directories, use:

echo .*/

Be careful: this includes `.` and `..` (current and parent directories). You can filter them out:

for d in */ .*/; do [ -d "$d" ] && [ "$d" != "." ] && [ "$d" != ".." ] && echo "$d"; done

Using Stat Command

The `stat` command shows file details. Combined with `find` or `ls`, it’s less common but works:

stat -c '%F %n' * | grep directory | cut -d' ' -f2-

This prints the file type and name, then filters for “directory”. It’s overkill for most tasks.

Common Pitfalls And Solutions

Hidden Directories Not Showing

By default, `ls -d */` ignores hidden directories (those starting with a dot). To include them:

ls -d .*/

This shows hidden folders like `.config` and `.cache`. But it also includes `.` and `..`. To exclude those:

ls -d .!(.|) 2>/dev/null

This uses extended globbing. Enable it with `shopt -s extglob` if needed.

Recursive Listing

If you need directories from subdirectories too, `find` is your best bet. The `ls` command alone can’t do recursion for this purpose.

For `ls`, you can use `**/` with globstar:

shopt -s globstar
ls -d **/

This lists all directories recursively. It’s slow on large file systems.

Handling Spaces In Directory Names

Directory names with spaces can break simple scripts. Always quote variables:

for d in */; do echo "$d"; done

This handles spaces correctly. The `find` command with `-print0` and `xargs -0` is safest for automation.

Practical Examples

Count Directories

To count how many directories are in a folder:

ls -d */ | wc -l

Or with `find`:

find . -maxdepth 1 -type d | wc -l

Subtract 1 if you don’t want to count the current directory (`.`).

List Directories With Sizes

To see directory sizes, use `du`:

du -sh */

This shows human-readable sizes for each directory. Combine with `sort` to find largest:

du -sh */ | sort -rh

Export To A File

Save the list to a text file:

ls -d */ > directories.txt

Or with full paths:

find "$PWD" -maxdepth 1 -type d > full_paths.txt

Use In Scripts

Here’s a simple script to back up all directories:

#!/bin/bash
for dir in */; do
    tar -czf "${dir%/}.tar.gz" "$dir"
done

This creates a tar archive for each directory. The `%/` removes the trailing slash.

Advanced Techniques

Using Awk For Custom Output

With `ls -la`, you can format output:

ls -la | awk '/^d/ {print $NF}'

This prints the last field (filename) of lines starting with `d`. It works but fails with symlinks or special characters.

Using Perl One-Liners

For complex filtering, Perl is powerful:

perl -e 'opendir(D, "."); while(readdir D) { print "$_\n" if -d && !/^\.\.?$/ }'

This lists all directories except `.` and `..`. It’s portable across Unix systems.

Using Python

If you have Python installed:

python -c "import os; [print(d) for d in os.listdir('.') if os.path.isdir(d)]"

This is cross-platform and handles edge cases well.

Performance Considerations

On systems with millions of files, `ls -d */` can be slow because the shell expands the pattern. The `find` command is often faster for large directories because it uses system calls directly.

For very large file systems, consider:

find . -maxdepth 1 -type d -printf '%f\n'

This prints only the directory name, reducing output processing.

Frequently Asked Questions

How Do I List Only Directories In Linux Including Hidden Ones?

Use `ls -d .*/ */` to show both hidden and non-hidden directories. Or use `find . -maxdepth 1 -type d` which includes hidden by default.

What’s The Difference Between `Ls -D */` And `Find . -Type D`?

`ls -d */` lists only top-level directories in the current folder. `find . -type d` lists all directories recursively, including subdirectories. Use `-maxdepth 1` with `find` to limit depth.

Can I List Directories Without Using `Ls`?

Yes. Use `echo */`, `find`, `tree -d`, or `stat`. Each has its own output format and use case.

How Do I List Only Empty Directories?

Use `find . -type d -empty`. This finds directories that contain no files or subdirectories.

Why Does `Ls -D */` Show Nothing Sometimes?

This happens when the current directory has no subdirectories. The shell pattern `*/` doesn’t match anything, so `ls` receives no arguments and shows nothing. Use `shopt -s nullglob` to handle this gracefully in scripts.

Putting It All Together

Now you know multiple ways to list only directories in Linux. Start with `ls -d */` for quick checks. Use `find` for recursive searches or when you need precise control. Combine with `grep`, `awk`, or other tools for custom output.

Remember these key points:

  • The `-d` flag in `ls` prevents directory content listing
  • The `*/` pattern matches directories in shell expansion
  • `find -type d` is the most flexible option
  • Always quote variables to handle spaces
  • Hidden directories require special handling

Practice these commands in a test directory. Create some folders with `mkdir test1 test2 “test 3″`, then try each method. You’ll quickly see which one fits your workflow.

For daily use, I recommend aliasing your favorite method. Add this to your `.bashrc`:

alias lsd='ls -d */'

Then just type `lsd` to list directories instantly. It saves time and keystrokes.

With these techniques, you’ll navigate Linux file systems more efficiently. No more scrolling through long file lists. Just clean, directory-only output whenever you need it.

Experiment with the commands, combine them with other tools, and make them your own. The terminal is your workshop—these are your tools.