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.
When working with file systems in programming or managing server environments, it's often necessary to list all subdirectories within a current directory. This task can be performed across various programming languages and command-line interfaces. Below, we'll explore how to achieve this in a few different environments and understand related concepts.
Understanding Directories
A directory, often referred to as a folder, is a file system structure used to organize files and other directories. When you list subdirectories, you’re essentially navigating the hierarchy of the file system to retrieve only the directories within a specific directory, ignoring the files.
Listing Subdirectories in UNIX/Linux
In UNIX-like systems, including Linux and macOS, the ls and find commands are commonly used in the terminal.
Using the ls command:
This command lists all directories and subdirectories in the current directory. The -d option specifies that directories are to be listed as opposed to their contents, and the */ pattern ensures that only directories are selected.
Using the find command:
Here, . signifies the current directory, -maxdepth 1 ensures that find searches only one level beneath the current directory (i.e., the current directory itself), and -type d restricts the search to directories.
Listing Subdirectories in Windows Command Line
In Windows Command Prompt (CMD), you can use the dir command:
The /Ad attribute specifies that only directories should be listed. This will display directories along with some additional details like the modification date.
Using Python for Directory Listing
Python offers a straightforward and powerful way to handle directories and files with the os module. Here is a basic example:
This script lists all subdirectories in the current working directory. os.listdir() returns everything in the directory (files and subdirectories), and os.path.isdir() checks if the item is a directory.
Using Node.js to List Subdirectories
In Node.js, you would typically use the fs module as follows:
Here, fs.readdir with the option { withFileTypes: true } allows us to use the isDirectory() method to check if an entry in the list is a directory.
Summary Table
| Environment | Command/Code | Description |
| UNIX/Linux | ls -d */ | Lists all subdirectories in the current directory. Uses shell globbing. |
| UNIX/Linux | find . -maxdepth 1 -type d | Finds all subdirectories in the current directory without recursing deeper. |
| Windows CMD | dir /Ad | Lists directories in the current directory using the DIR command. |
| Python | See example code | Uses os module to list directories in Python scripts. |
| Node.js | See example code | Utilizes fs and path modules to list directories in JavaScript environment. |
Additional Considerations
When listing directories, it's important to handle exceptions and errors such as permission issues, especially in multi-user environments or public servers. Moreover, the output and specifics might differ slightly based on system configurations and the presence of symbolic links.
In conclusion, listing all subdirectories in a certain directory is a fundamental task that can be accomplished in several ways depending on your working environment. Understanding how to do this efficiently allows better manipulation and organization of file systems in many applications.

