Running `ls -d */` returns only the directory names within a location, filtering out regular files from the view. If you are wondering how to list directories in linux, this command is your quickest answer. But there are many more ways to do it, each useful for different situations.
Whether you are a beginner or a seasoned sysadmin, knowing how to list directories in linux saves time and helps you navigate the file system efficiently. This guide covers every method, from basic commands to advanced filtering.
How To List Directories In Linux
Listing directories in Linux is a core skill. The `ls` command is the primary tool, but it has many options. You can list only directories, show hidden ones, or display them in a tree structure. Let’s start with the simplest methods and build up.
Using Ls With The -D Flag
The `-d` flag tells `ls` to list directories themselves, not their contents. Combine it with a pattern like `*/` to match only directories.
- Command:
ls -d */ - What it does: Lists all directories in the current folder, one per line.
- Example output:
Documents/ Downloads/ Pictures/
This method works in any shell and is fast. It does not show hidden directories (those starting with a dot).
Listing Hidden Directories
Hidden directories start with a dot, like `.config` or `.ssh`. To include them, add the `-a` flag.
Command: ls -d .*/
This lists only hidden directories. Combine with `*/` to see both hidden and regular directories: ls -d .*/ */
Be careful: `ls -d .*/` also shows `.` (current directory) and `..` (parent directory). To exclude them, use ls -d .[!.]*/ (works in bash).
Using Ls With The -L Flag
The `-l` flag gives a long listing format with details like permissions, owner, size, and modification date. To list only directories in long format:
Command: ls -ld */
This shows directory details without entering them. Example output:
drwxr-xr-x 2 user user 4096 Jan 15 10:30 Documents/
drwxr-xr-x 3 user user 4096 Jan 14 09:15 Downloads/
The first character `d` confirms it is a directory.
Listing Directories Recursively
Sometimes you need to see directories inside subdirectories. The `-R` flag does this.
Command: ls -R | grep ':$' | sed 's/:$//'
This lists all directories recursively, including nested ones. The `grep` and `sed` part filters only directory names from the recursive output.
A simpler alternative is find . -type d, which we cover next.
Using Find Command To List Directories
The `find` command is more powerful than `ls` for complex searches. It can list directories based on name, size, date, or permissions.
Basic Find For Directories
Command: find . -type d
This lists all directories starting from the current location. It shows full paths relative to the starting point.
- `.` means start here.
- `-type d` filters for directories only.
To list only the top-level directories, add `-maxdepth 1`:
Command: find . -maxdepth 1 -type d
Find With Name Pattern
You can search for directories with specific names. For example, to find all directories named “backup”:
Command: find / -type d -name "backup"
Use `-iname` for case-insensitive matching.
Find By Modification Time
To list directories modified in the last 7 days:
Command: find . -type d -mtime -7
This is useful for finding recently changed folders.
Using Grep With Ls Output
If you prefer using `ls` but want to filter results, pipe its output to `grep`.
Filtering With Grep
Command: ls -l | grep '^d'
The `^d` pattern matches lines starting with “d”, which indicates a directory in long listing. This works but is less direct than `ls -d */`.
For a cleaner list of directory names:
Command: ls -l | grep '^d' | awk '{print $NF}'
This extracts the last column (the directory name).
Using Tree Command
The `tree` command displays directories and files in a tree-like structure. It is not installed by default on all systems, but you can install it via your package manager.
Installing Tree
- Ubuntu/Debian:
sudo apt install tree - Fedora:
sudo dnf install tree - macOS (Homebrew):
brew install tree
Listing Only Directories With Tree
Command: tree -d
This shows only directories in a tree format. Add `-L 2` to limit depth:
Command: tree -d -L 2
The output is visual and easy to read, especially for large projects.
Using Wildcards And Brace Expansion
Shell wildcards can help list directories without extra commands.
Using */ Pattern
As shown earlier, `*/` matches directories because files do not end with a slash. You can use this with `echo`:
Command: echo */
This prints all directory names on one line. For a list with one per line, use printf '%s\n' */.
Brace Expansion For Multiple Patterns
If you want to list directories matching multiple patterns, use braces:
Command: ls -d {Documents,Downloads}*/
This lists directories starting with “Documents” or “Downloads”.
Listing Directories In Specific Locations
You are not limited to the current directory. Specify any path.
Absolute Path Example
Command: ls -d /var/log/*/
This lists all directories inside `/var/log`.
Relative Path Example
Command: ls -d ../*/
This lists directories in the parent folder.
Using Stat And Other Tools
For advanced users, `stat` shows detailed metadata about directories.
Stat For Directory Info
Command: stat -c '%n' */
This prints only directory names. The `-c` flag formats the output.
You can also use `du` to list directories with their disk usage:
Command: du -sh */
This shows human-readable sizes for each directory.
Common Mistakes And Tips
Here are some pitfalls to avoid when listing directories.
Forgetting The Slash
Without the trailing slash, `ls -d *` lists both files and directories. Always use `*/` to filter.
Hidden Directories Overlooked
Most methods ignore dot directories. Use `.*/` to include them.
Case Sensitivity
Linux is case-sensitive. `ls -d */` does not match `MyDir/` if you type `mydir/`.
Scripting With Directory Lists
You can use directory listing in scripts to automate tasks.
Looping Over Directories
Example bash script:
for dir in */; do
echo "Processing $dir"
# Do something
done
This iterates over each directory in the current folder.
Counting Directories
Command: ls -d */ | wc -l
This counts the number of directories.
Performance Considerations
For directories with thousands of items, `find` is faster than `ls` with piping. Use `find . -maxdepth 1 -type d` for large directories.
Avoid using `ls -R` on huge file systems; it can be slow and produce massive output.
Faq
How Do I List Only Directories In Linux Using Ls?
Use ls -d */ to list only directories in the current location. Add -a to include hidden ones.
What Is The Difference Between Ls -D */ And Find . -Type D?
ls -d */ lists only top-level directories. find . -type d lists all directories recursively, including subdirectories.
Can I List Directories By Size In Linux?
Yes, use du -sh */ | sort -h to list directories sorted by size.
How To List Directories With Full Path?
Use find $(pwd) -type d -maxdepth 1 or ls -d $PWD/*/ to get absolute paths.
Why Does Ls -D */ Show A Slash At The End?
The slash indicates it is a directory. It helps distinguish directories from files in scripts.
Conclusion
Now you know multiple ways to list directories in Linux. The `ls -d */` command is the fastest for simple tasks. For recursive searches, `find . -type d` is your friend. Use `tree -d` for a visual overview. Practice these commands to become more efficient on the command line. Each method has its place, so choose the one that fits your workflow best.
Remember to check your shell and system settings if a command does not work as expected. Most Linux distributions include these tools by default. Happy directory listing!