How To List All Directories In Linux : Linux Directory Listing Commands

Listing all directories in Linux is one of the first commands you’ll use to navigate the file system. If you’re wondering how to list all directories in linux, you’ve come to the right place. This guide will show you multiple ways to do it, from simple commands to advanced filtering. Whether you’re a beginner or a seasoned sysadmin, these methods will help you manage your files efficiently.

The Linux command line gives you powerful tools to see only directories, not files. You can use ls, find, tree, and even grep to get the job done. Let’s start with the basics and build up to more complex techniques.

How To List All Directories In Linux

This section covers the most common and effective ways to list directories. You’ll learn commands that work on any Linux distribution, from Ubuntu to CentOS. Each method has its own strengths, so pick the one that fits your task.

Using The Ls Command With Options

The ls command is the go-to for listing directory contents. To show only directories, add the -d flag with a pattern. Run ls -d */ in your terminal. This lists all directories in the current folder, ending with a slash.

For a more detailed view, use ls -l | grep "^d". The ls -l output starts with a “d” for directories. Grep filters those lines. You’ll see permissions, owner, size, and name. This method works well for quick checks.

Another variant is ls -la | grep "^d". The -a flag shows hidden directories (those starting with a dot). Hidden folders like .config or .ssh appear too. Use this when you need a complete list.

Using The Find Command For Recursive Listing

The find command is perfect for listing directories recursively. Run find . -type d to show all directories under the current path. This includes subdirectories, making it ideal for deep file systems.

To limit the depth, use find . -maxdepth 1 -type d. This shows only immediate subdirectories, not nested ones. You can change the number to control how deep the search goes. For example, -maxdepth 2 goes two levels down.

You can also combine find with other commands. For instance, find /home -type d -name "Documents" finds all folders named “Documents” under /home. This is useful for locating specific directories.

Using The Tree Command For Visual Output

The tree command gives a visual tree of directories. Install it first with sudo apt install tree on Debian-based systems or sudo yum install tree on Red Hat-based ones. Then run tree -d to see only directories.

You can customize the output. Use tree -d -L 2 to show directories up to two levels deep. The -L flag controls depth. Add -a to include hidden directories. The tree format makes it easy to understand the folder structure at a glance.

For a cleaner look, try tree -d -C. The -C flag adds color, making directories stand out. This is great for presentations or when you need a quick overview.

Using Grep With Ls For Filtering

Grep is a text filter that works well with ls. As mentioned, ls -l | grep "^d" lists directories. You can also use ls -p | grep "/". The -p flag adds a slash to directories, and grep filters those lines.

Another trick is ls -F | grep "/". The -F flag appends a slash to directories, similar to -p. This is a quick way to see directories without extra details. It’s useful for scripting or when you need a simple list.

For hidden directories, use ls -la | grep "^d" or ls -pA | grep "/". The -A flag shows almost all entries except . and ... This keeps the output clean while including hidden folders.

Advanced Techniques For Listing Directories

Once you know the basics, you can use more advanced methods. These techniques save time when working with large file systems or specific conditions.

Using Find With Size Or Date Filters

The find command can filter by size or modification date. For example, find . -type d -size +10M lists directories larger than 10 MB. Note that this checks the directory’s metadata size, not its contents. For content size, use du instead.

To find directories modified in the last 7 days, run find . -type d -mtime -7. The -mtime flag checks modification time. Use +7 for older than 7 days. This is helpful for cleanup tasks.

You can combine filters. For instance, find . -type d -name "temp" -mtime +30 finds “temp” directories older than 30 days. This automates finding stale folders.

Using The Du Command To List Directories With Sizes

The du command shows disk usage. To list directories with their sizes, run du -sh */. The -s flag gives a summary, and -h makes it human-readable (e.g., 1.2G). This lists only top-level directories.

For a recursive list, use du -h --max-depth=1. The --max-depth flag controls depth. Set it to 1 for immediate subdirectories. You can sort the output with sort -h to see largest directories first.

To include hidden directories, use du -sh .[!.]* */. This pattern matches hidden folders (except . and ..) and regular ones. It’s a bit tricky but works well.

Using The Ls Command With Wildcards

Wildcards help filter directory names. For example, ls -d */ lists all directories. To find directories starting with “proj”, use ls -d proj*/. The asterisk matches any characters.

You can also use ls -d *[0-9]*/ to list directories with numbers in their names. This is useful for versioned folders. Combine with ls -d *2024*/ for year-specific directories.

For case-insensitive matching, enable shopt -s nocaseglob first. Then ls -d [a-z]*/ lists directories starting with any letter, regardless of case. This is handy when you’re unsure about naming conventions.

Common Mistakes And Troubleshooting

Even experienced users make errors when listing directories. Here are common pitfalls and how to avoid them.

Forgetting Hidden Directories

Many commands don’t show hidden directories by default. The ls -d */ command omits folders starting with a dot. Always use -a or -A flags when you need a complete list. Hidden directories often contain configuration files, so missing them can cause issues.

For find, hidden directories are included by default. But if you use ls with grep, remember to add -a. For example, ls -la | grep "^d" includes hidden ones. Double-check your output if something seems missing.

Permission Denied Errors

You might see “Permission denied” when listing directories in system folders like /root or /etc. Use sudo to override. For instance, sudo ls -d /root/*/ lists directories in root’s home. Be careful with sudo; it gives full access.

If you get errors with find, use find / -type d 2>/dev/null. The 2>/dev/null suppresses error messages. This keeps the output clean. You can also use sudo find / -type d to avoid permission issues.

Misunderstanding Output Format

The ls -l | grep "^d" command shows detailed info, but the directory name is at the end. If you only need names, use ls -d */ or find . -type d -printf "%f\n". The -printf flag in find gives just the name.

Another common mistake is confusing directories with files. The ls -l output starts with “d” for directories and “-” for files. Always check the first character. If you see “l”, it’s a symbolic link, not a directory.

Practical Examples And Use Cases

Let’s look at real-world scenarios where listing directories is essential. These examples show how to apply the commands in daily work.

Listing All Directories In A Project

Suppose you have a project with multiple subfolders. Run find . -type d -maxdepth 3 to see the structure up to three levels deep. This helps you understand the layout without opening each folder.

For a web project, use ls -d */ to list top-level directories like css, js, and images. Add tree -d for a visual map. This is useful when onboarding new team members.

Finding Large Directories For Cleanup

To find directories consuming disk space, use du -sh */ | sort -h. This lists directories with sizes, sorted from smallest to largest. Look for directories over 1 GB. Then use find . -type d -size +100M to confirm.

For a system-wide check, run sudo du -sh /* | sort -h. This shows sizes of top-level directories like /var and /home. Identify large folders and clean them up with rm -rf (carefully).

Listing Directories By Name Pattern

If you need directories with “backup” in the name, use find . -type d -name "*backup*". The asterisks match any characters before or after “backup”. This finds all backup folders, regardless of location.

For case-insensitive search, use -iname instead of -name. For example, find . -type d -iname "*Backup*" matches “Backup”, “backup”, or “BACKUP”. This is useful when naming conventions vary.

Frequently Asked Questions

Here are common questions about listing directories in Linux. These cover variations of the main keyword and address specific needs.

How do I list only directories in the current folder?

Use ls -d */ or find . -maxdepth 1 -type d. Both show only directories in the current directory. The ls command is faster for small lists.

Can I list directories with their sizes?

Yes, use du -sh */ for top-level directories or du -h --max-depth=1 for recursive listing. The du command shows disk usage in human-readable format.

How do I list all directories including hidden ones?

Run ls -d .*/ */ or find . -type d. The ls command with .*/ shows hidden directories, while find includes them by default.

What command lists directories recursively?

Use find . -type d to list all directories recursively. For a visual tree, install and run tree -d. Both show subdirectories at all levels.

How do I list directories modified in the last week?

Use find . -type d -mtime -7. The -mtime -7 flag filters directories modified within the last 7 days. Adjust the number for different time frames.

Final Tips For Listing Directories Efficiently

Practice these commands to build muscle memory. Start with ls -d */ for quick checks. Use find . -type d for deep searches. Combine with grep or du for advanced tasks.

Create aliases in your .bashrc file for frequently used commands. For example, alias lsd='ls -d */' saves typing. This boosts your productivity over time.

Remember to check permissions and hidden directories. A missing directory can cause errors in scripts or backups. Always verify your output with ls -la or tree -a when needed.

With these techniques, you can list all directories in Linux quickly and accurately. Whether you’re managing a server or organizing personal files, these commands will serve you well. Experiment with different options to find what works best for your workflow.