In this short guide, we will learn how to list only directories using the ls command in Bash and Linux terminals. This essential skill helps developers and system administrators quickly navigate file systems, identify folder structures, and manage projects efficiently.
Here you can find the short answer:
*(1) List directories with ls -d /
ls -d */
(2) List directories with ls -l and grep
ls -l | grep "^d"
So let's see different methods to list directories in Bash.
Problem: Filtering Directories from ls Output
The standard ls command shows both files and directories together, making it difficult to:
- Quickly identify folder structures
- Count subdirectories in a project
- Navigate complex directory trees
- Write shell scripts that process only directories
Solution: Use specific ls flags and filters to display directories only.
1: Using ls -d */ (Recommended Method)
The fastest and most reliable method to list only directories:
ls -d */
Output Result:
images/
documents/
projects/
backups/
downloads/
How it works:
*/- Wildcard pattern matching directories only-d- List directories themselves, not their contents- Displays directory names with trailing slashes
Real-world example: Quickly viewing project folders in a development workspace containing both source files and directories.
2: Using ls -l with grep
Filter ls -l output to show only directory entries:
ls -l | grep "^d"
Output Result:
drwxr-xr-x 5 user staff 160 Dec 15 10:30 images
drwxr-xr-x 3 user staff 96 Dec 14 09:15 documents
drwxr-xr-x 8 user staff 256 Dec 13 14:22 projects
drwxr-xr-x 2 user staff 64 Dec 12 11:45 backups
drwxr-xr-x 4 user staff 128 Dec 11 16:30 downloads
Command breakdown:
ls -l- Long listing format with details|- Pipe output to grepgrep "^d"- Filter lines starting with 'd' (directory indicator)- Shows permissions, owner, size, and modification date
Use case: Checking directory permissions and modification dates for backup or security audits.
3: Using find Command (Advanced)
For recursive directory listing or more complex filters:
find . -maxdepth 1 -type d
Output Result:
.
./images
./documents
./projects
./backups
./downloads
Command options:
.- Search in current directory-maxdepth 1- Don't search subdirectories-type d- Match directories only- Returns full paths including current directory (
.)
Exclude current directory:
find . -maxdepth 1 -type d -not -path '.'
Real-world example: Listing client project folders in a web development directory while excluding the parent folder.
4: List Directories with Full Details
Combine methods for detailed directory information:
ls -ld */
Output Result:
drwxr-xr-x 5 user staff 160 Dec 15 10:30 images/
drwxr-xr-x 3 user staff 96 Dec 14 09:15 documents/
drwxr-xr-x 8 user staff 256 Dec 13 14:22 projects/
drwxr-xr-x 2 user staff 64 Dec 12 11:45 backups/
drwxr-xr-x 4 user staff 128 Dec 11 16:30 downloads/
Flags explained:
-l- Long format with permissions and timestamps-d- List directories, not contents*/- Match only directories
Use case: Auditing folder permissions before deploying to production servers.
5: Count Number of Directories
Count subdirectories in current location:
ls -d */ | wc -l
Output Result:
5
Or with find:
find . -maxdepth 1 -type d | wc -l
Output Result:
6
Note: find includes the current directory (.) in the count, so subtract 1 for actual subdirectory count.
Advanced Options
List Directories Sorted by Modification Time
ls -ltd */
Shows most recently modified directories first.
List Hidden Directories
ls -d .*/
Output Result:
./
../
.git/
.config/
.cache/
List Directories Recursively
find . -type d
Shows all subdirectories at all levels.
List Only Directory Names (No Details)
ls -1d */
Output Result:
images/
documents/
projects/
backups/
downloads/
Common Use Cases
Project Management: List client folders, project directories, repository structures
System Administration: Audit log directories, backup folders, configuration paths
Web Development: Navigate website sections, asset folders, component directories
Data Science: Identify dataset folders, model directories, output paths
DevOps: Check deployment folders, container volumes, build directories
Performance Tips
✅ Use ls -d */ for fastest results in most cases
✅ Use find when you need recursive listing or complex filters
✅ Combine with grep for pattern matching (e.g., ls -d */ | grep project)
✅ Add -1 flag for one directory per line in scripts:
ls -1d */
✅ Use --color=auto for visual distinction in terminal:
ls -d --color=auto */
❌ Avoid parsing ls -l output in scripts - use find instead for reliability
❌ Don't use ls for scripting with spaces in filenames - prefer find -print0 with xargs -0
Quick Reference Table
| Command | Purpose | Output Type |
|---|---|---|
ls -d */ |
List directories | Simple names with / |
ls -ld */ |
List with details | Permissions, dates, sizes |
ls -l | grep "^d" |
Filter directories | Full details |
find . -maxdepth 1 -type d |
Find directories | Full paths |
ls -d */ | wc -l |
Count directories | Number only |
Troubleshooting
Problem: "No such file or directory" error
Solution: Current directory has no subdirectories. Verify with ls -la.
Problem: Command shows files too
Solution: Ensure you use */ pattern or -type d with find.
Problem: Hidden directories not shown
Solution: Use ls -d .*/ */ to include both hidden and visible directories.