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:
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:
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:
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:
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:
Both are fine. pathlib is often easier to read.
Recursive Listing In Python
For a recursive walk, pathlib stays concise:
If you want more control over the walk, os.walk is the traditional tool:
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
findin shell scripts - use
os.scandirfor fast direct children in Python - use
pathlibwhen readability matters - use
os.walkwhen recursive control matters
That choice matters more than memorizing one magic command.
Hidden Directories And Symbolic Links
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:
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,
findis the most reliable general solution. - In Python,
os.scandirandpathlibare good direct-child options. - Use
os.walkorPath.rglobfor recursive listing. - Be explicit about hidden directories and symlink behavior when correctness matters.

