How to List Directories in Bash
-
Using the
ls
Command -
Using the
find
Command -
Using the
tree
Command -
Using the
bash
built-incompgen
- Conclusion
- FAQ

When working in a Bash environment, knowing how to list directories is essential for efficient navigation and file management. Whether you’re a seasoned developer or just starting your journey in command-line interfaces, mastering directory listing can save you time and effort.
In this article, we’ll explore several methods to list directories in Bash, complete with code examples and detailed explanations. By the end, you’ll be equipped with the knowledge to navigate your system like a pro. So, let’s dive in!
Using the ls
Command
The most common way to list directories in Bash is by using the ls
command. This command provides a simple and straightforward method for viewing files and directories in your current working directory. You can also customize the output to show only directories, making it a versatile option.
ls -d */
This command uses the -d
option, which tells ls
to list directories themselves rather than their contents. The */
pattern ensures that only directories are matched. The result will be a neat list of all directories in the current folder.
Output:
Documents/
Downloads/
Pictures/
Videos/
This command is particularly useful when you want a quick overview of your directory structure without the clutter of files. You can also combine it with other options for more detailed information. For example, adding -l
will give you a long listing format that includes permissions, ownership, and modification dates. This can be helpful for assessing the properties of each directory at a glance.
Using the find
Command
Another powerful way to list directories in Bash is by using the find
command. This command is particularly useful for searching through nested directories. If you want to find all directories within a specific path, find
is your best bet.
find . -type d
In this command, .
refers to the current directory, and -type d
specifies that you want to find directories only. This command will recursively search through all subdirectories and display a list of every directory it finds.
Output:
.
./Documents
./Downloads
./Pictures
./Videos
The find
command is incredibly flexible. You can modify it to search for directories that match specific criteria. For instance, if you want to list only directories that were modified in the last 7 days, you could use the -mtime
option:
find . -type d -mtime -7
This command will filter the results to show only those directories that have been changed recently, making it a great tool for monitoring your file system.
Using the tree
Command
If you’re looking for a more visual representation of your directory structure, the tree
command is an excellent option. This command displays directories in a tree-like format, making it easy to see the hierarchy of your files and folders.
tree -d
The -d
flag tells tree
to display only directories. This command will produce an organized view of your directory structure, which can be very helpful for understanding complex hierarchies.
Output:
.
├── Documents
├── Downloads
├── Pictures
└── Videos
The tree
command is not installed by default on all systems, so you might need to install it first. Once installed, it offers a clear and concise way to visualize your directories. You can also customize the output further by using additional flags, such as -L
to limit the depth of the tree displayed.
Using the bash
built-in compgen
If you prefer a more programmatic approach, you can use the compgen
built-in command in Bash to list directories. This method is particularly useful in scripts where you might need to process directory names further.
compgen -d
This command will generate a list of all directories in your current environment. The output will be a simple list of directory names, which you can then manipulate or use in other commands.
Output:
Documents
Downloads
Pictures
Videos
Using compgen
allows for more flexibility in scripting, as you can easily capture the output and use it in loops or other conditional statements. This makes it a powerful tool for automated tasks where directory management is necessary.
Conclusion
Listing directories in Bash is a fundamental skill that can significantly enhance your productivity. Whether you prefer the straightforward ls
command, the powerful find
command, the visual appeal of tree
, or the scripting capabilities of compgen
, each method has its unique advantages. By mastering these commands, you’ll be better equipped to navigate your file system and manage your projects efficiently. So, get out there and start exploring your directories like a Bash expert!
FAQ
-
How do I list directories in a specific path?
You can specify the path in thels
orfind
command. For example,ls -d /path/to/directory/*/
orfind /path/to/directory -type d
. -
Can I list hidden directories in Bash?
Yes, you can usels -d .*/
to list hidden directories that start with a dot. -
What if I want to list directories and their sizes?
You can use thedu -sh */
command to list directories along with their sizes in a human-readable format. -
Is the
tree
command available by default?
Thetree
command is not installed by default on all systems. You may need to install it via your package manager. -
Can I list directories recursively?
Yes, you can usefind . -type d
to list directories recursively, ortree -d
for a visual representation.
Related Article - Bash File
- How to Write to a File in Bash
- How to Overwrite File in Bash
- How to Open HTML File Using Bash
- How to Search for Files With a Filename Beginning With a Specified String in Bash
- How to Check if a File Is Empty in Bash
- How to Read File Into Variable in Bash