directory list
subdirectories
file management
coding
tutorials

Getting a list of all subdirectories in the current directory

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Listing subdirectories sounds simple, but the right approach depends on two details: whether you want only immediate children or a recursive walk, and whether you are doing it in the shell or in code. Mixing those cases is why many short answers become confusing.

Immediate Subdirectories In The Shell

If you only want the directories directly inside the current directory, shell tools are usually enough.

On Unix-like systems, find is the most reliable option:

bash
find . -mindepth 1 -maxdepth 1 -type d

This means:

  • start at the current directory .
  • do not include the current directory itself
  • do not descend beyond one level
  • return only directories

A shorter shell-only pattern is:

bash
printf '%s\n' */

That works in many shells, but it is less explicit and behaves differently when no directory matches. For scripts, find is safer.

Recursive Directory Listing

If you want every nested subdirectory, remove the max depth restriction:

bash
find . -mindepth 1 -type d

Now find walks the tree recursively. This is useful for audits, cleanup tools, or recursive build scripts.

If you need only names relative to the current directory, you can post-process the output or use the path values directly in your program.

Doing It In Python

For Python code, the two most common choices are os.scandir and pathlib.

For immediate children, os.scandir is efficient and straightforward:

python
1import os
2
3subdirs = [entry.name for entry in os.scandir('.') if entry.is_dir()]
4print(subdirs)

This is usually better than calling os.listdir and then os.path.isdir separately, because scandir exposes directory metadata more efficiently.

If you prefer modern path objects:

python
1from pathlib import Path
2
3subdirs = [p.name for p in Path('.').iterdir() if p.is_dir()]
4print(subdirs)

Both are fine. pathlib is often easier to read.

Recursive Listing In Python

For a recursive walk, pathlib stays concise:

python
1from pathlib import Path
2
3subdirs = [str(p) for p in Path('.').rglob('*') if p.is_dir()]
4print(subdirs)

If you want more control over the walk, os.walk is the traditional tool:

python
1import os
2
3for root, dirs, files in os.walk('.'):
4    for name in dirs:
5        print(os.path.join(root, name))

This is useful when you also need access to files or want to skip certain directories.

Which Option Should You Use?

A practical rule is:

  • use find in shell scripts
  • use os.scandir for fast direct children in Python
  • use pathlib when readability matters
  • use os.walk when recursive control matters

That choice matters more than memorizing one magic command.

Another thing short answers skip is what counts as a directory. Hidden directories such as .git are still directories, and most filesystem APIs will return them unless you filter them out.

Symbolic links are more subtle. Some tools treat a symlink to a directory as a directory depending on the API and flags used. If your script must avoid following links, make that requirement explicit.

For example, with find, you can stay conservative by not enabling link following. In Python, check the behavior of is_dir() for your use case and whether symlinks should be included.

A Small Reusable Helper

A helper function keeps the direct-child case clean:

python
1from pathlib import Path
2
3
4def list_subdirectories(path='.'):
5    return [p for p in Path(path).iterdir() if p.is_dir()]
6
7
8for directory in list_subdirectories():
9    print(directory)

This is runnable, readable, and easy to extend with filtering rules later.

Common Pitfalls

The most common mistake is using a recursive method when you only wanted immediate subdirectories. That can produce a lot more results than expected.

Another mistake is using shell globbing such as */ inside automation without checking how the shell behaves when nothing matches.

Developers also forget hidden directories, which can affect tools that accidentally traverse .git, .venv, or build output folders.

Finally, do not use os.listdir plus repeated path checks in performance-sensitive code if os.scandir or pathlib would be clearer and more efficient.

Summary

  • Decide first whether you want direct children or a recursive walk.
  • In the shell, find is the most reliable general solution.
  • In Python, os.scandir and pathlib are good direct-child options.
  • Use os.walk or Path.rglob for recursive listing.
  • Be explicit about hidden directories and symlink behavior when correctness matters.

Course illustration
Course illustration

All Rights Reserved.