15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
02.09.2025

How to List Directories in Linux: A Complete Guide for Beginners and Sysadmins

Working in Linux often feels like navigating a vast library. Instead of shelves, you have directories (folders), and instead of books, you have files. Whether you are a seasoned system administrator, a developer, or a curious beginner just getting started, mastering the fundamentals of filesystem navigation is non-negotiable. One of the most critical foundational skills is knowing how to list directories efficiently and accurately.

This guide walks you through every practical method for listing directories in Linux, explains the strengths and limitations of each approach, and shows you exactly when to use which tool β€” including in scripting and production server environments.

Why Listing Directories Matters

On a personal computer, you might occasionally browse your "Documents" or "Downloads" folder through a graphical interface. On a Linux server, however, directories are at the heart of nearly every administrative task you will perform:

  • System administration: Quickly audit what lives in /etc/, /var/log/, or /usr/local/bin/.
  • Web hosting: Locate and manage project folders under /var/www/ or /home/.
  • Software development: Identify version control and environment directories such as .git/ or .venv/.
  • Security auditing: Confirm exactly what is installed, configured, and running on the system.
  • Automation and scripting: Reliably enumerate directories for use in shell scripts and cron jobs.

If you cannot list directories efficiently, you will always feel disoriented β€” especially on a remote VPS Hosting environment where there is no graphical file manager to fall back on.

Method 1: The ls Command β€” Fast and Simple

The ls command is the default tool for displaying the contents of a directory. It is the first command most Linux users learn, and for good reason: it is fast, human-readable, and available on every Unix-like system.

Basic usage

ls

This lists all non-hidden files and directories in the current working directory.

List only directories

ls -d */

Here is what each part does:

  • -d β€” Tells ls not to descend into subdirectories, but to list the directory entries themselves.
  • */ β€” A shell glob pattern that matches all directories (and symbolic links to directories) in the current folder.

List directories with detailed information

ls -ld */

Adding -l gives you a long listing format that includes:

  • File permissions
  • Number of hard links
  • Owner and group
  • File size
  • Last modification timestamp

Example output:

drwxr-xr-x 3 root root 4096 Jun 10 14:22 backups/
drwxr-xr-x 5 www-data www-data 4096 Jun 12 09:15 html/
drwxr-xr-x 2 deploy deploy 4096 Jun 11 18:03 logs/

Limitation: Hidden directories are excluded

The */ glob does not match hidden directories β€” those whose names begin with a dot (.), such as .git/, .ssh/, or .config/. This is an important caveat, especially in security-sensitive environments.

When to use ls -d */: Quick, interactive checks where you only need visible directories and do not require scripting reliability.

Method 2: The find Command β€” Powerful and Reliable

The find command is the professional's choice for enumerating directories. Unlike ls, it is designed for programmatic use, supports recursive traversal, includes hidden directories by default, and behaves consistently across different Linux distributions and locales.

List all directories recursively

find . -type d
  • . β€” Start from the current directory.
  • -type d β€” Match only directory entries (not files, symlinks, or other types).

This will recursively list every directory at every depth level beneath your current location.

List only top-level directories (portable method)

find . -mindepth 1 -maxdepth 1 -type d
  • -mindepth 1 β€” Excludes the current directory itself (.) from the results.
  • -maxdepth 1 β€” Prevents recursion beyond the immediate children.

This is the most portable and reliable way to list only the direct subdirectories of the current folder, and it works identically on GNU/Linux, macOS, and BSD systems.

Alternative for GNU/Linux systems

On most modern Linux distributions β€” including those running on Dedicated Servers β€” you can also use:

find . -maxdepth 1 -type d

Note that this will include . itself in the output. Use -mindepth 1 alongside -maxdepth 1 to exclude it cleanly.

Include hidden directories

Because find does not rely on shell globs, it automatically includes hidden directories:

find . -mindepth 1 -maxdepth 1 -type d

This will show .git/, .ssh/, .config/, and any other dot-prefixed directories alongside visible ones.

Use find in scripts

find is the correct tool for shell scripting because:

  • It handles filenames with spaces and special characters safely.
  • It is not affected by locale settings or terminal color configurations.
  • It produces consistent, parseable output.

Example: Loop over all top-level directories

find . -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
    echo "Processing: $dir"
done

When to use find: Any time you need hidden directories included, recursive results, or reliable output for scripting and automation.

Method 3: The tree Command β€” Visual Directory Maps

The tree command renders a visual, hierarchical map of your directory structure. It is not installed by default on all distributions but is extremely useful for documentation, onboarding, and understanding complex project layouts.

Install tree

Debian/Ubuntu:

sudo apt install tree

CentOS/RHEL/AlmaLinux:

sudo yum install tree

List only directories (no files)

tree -d

Limit depth to avoid overwhelming output

tree -d -L 2
  • -d β€” Show directories only.
  • -L 2 β€” Limit the display to 2 levels deep.

Example output:

.
β”œβ”€β”€ backups
β”‚   └── daily
β”œβ”€β”€ html
β”‚   β”œβ”€β”€ assets
β”‚   └── uploads
└── logs

Include hidden directories

tree -d -a

When to use tree: Visualizing project structures, writing documentation, or exploring an unfamiliar server layout for the first time.

Method 4: Why You Should Avoid ls -l | grep "^d"

You will occasionally encounter this pattern suggested online:

ls -l | grep "^d"

The idea is to filter the long-listing output of ls to show only lines beginning with d β€” which indicates a directory. While this appears clever, it is fundamentally fragile and should be avoided in any serious context:

ProblemExplanation
Locale sensitivitySome locales or terminal configurations alter the output format of ls -l, breaking the grep pattern.
Color codesIf ls outputs ANSI color escape codes, the ^d pattern may fail to match.
Hidden directoriesNot shown, just like with ls -d */.
Symbolic linksSymlinks pointing to directories show as l, not d, so they are silently excluded.
Scripting unreliabilityParsing ls output in scripts is explicitly discouraged in shell scripting best practices.

Use find instead. It is purpose-built for this task and avoids all of the above pitfalls.

Quick Reference: Choosing the Right Method

GoalBest Command
Quick interactive check (visible dirs only)ls -d */
Detailed listing with permissionsls -ld */
Top-level dirs including hiddenfind . -mindepth 1 -maxdepth 1 -type d
All directories recursivelyfind . -type d
Visual tree structuretree -d
Use in a shell scriptfind . -mindepth 1 -maxdepth 1 -type d

Best Practices for Production Server Environments

When working on a live server β€” whether it is a Shared Web Hosting account or a fully managed VPS β€” keep these principles in mind:

  1. Always check hidden directories during security audits. Directories like .ssh/, .git/, .env/, and .config/ often contain sensitive credentials, keys, and configuration files. Use find with -mindepth 1 to ensure they appear in your output.
  1. Use find in all scripts, never ls. Parsing ls output is a well-known anti-pattern in shell scripting. The find command is deterministic, locale-independent, and handles edge cases gracefully.
  1. Combine find with -exec for bulk operations. For example, to list directory sizes:
   find . -mindepth 1 -maxdepth 1 -type d -exec du -sh {} ;
  1. Use tree -d -L 2 when onboarding. When you first SSH into an unfamiliar server, a quick tree -d -L 2 from /var/www/ or /home/ gives you an instant structural overview.
  1. Restrict permissions carefully. Knowing what directories exist is only part of the picture. Regularly audit directory permissions with ls -ld */ to catch misconfigured world-writable directories.

Practical Example: Auditing a Web Server Directory

Suppose you have just deployed a new application on a Linux VPS. Here is a practical workflow combining the commands covered in this guide:

# Navigate to the web root
cd /var/www/

# Quick visual check of top-level directories
ls -ld */

# Full audit including hidden directories
find . -mindepth 1 -maxdepth 1 -type d

# Visual map of the project structure (2 levels deep)
tree -d -L 2

# Check disk usage per directory
find . -mindepth 1 -maxdepth 1 -type d -exec du -sh {} ;

This workflow takes under a minute and gives you a complete picture of your web server's directory layout, permissions, and disk consumption β€” essential knowledge for any administrator managing a VPS with cPanel or a custom Linux stack.

Conclusion

Listing directories is one of the most fundamental Linux skills, yet the method you choose matters more than most beginners realize. Here is a summary of the key takeaways:

  • ls -d */ is perfect for fast, interactive checks β€” but it ignores hidden directories and is not suitable for scripting.
  • find . -mindepth 1 -maxdepth 1 -type d is the gold standard for reliable, portable, script-safe directory listing that includes hidden entries.
  • tree -d is invaluable for visualizing complex structures and communicating them to others.
  • Avoid ls -l | grep "^d" β€” it is fragile, locale-dependent, and has no advantages over find.

Whether you are managing configurations on a Dedicated Servers environment, deploying web applications, or simply exploring a new Linux machine for the first time, mastering these commands will save you time, prevent costly mistakes, and give you the confidence to navigate any filesystem with precision.

The command line is not a barrier β€” it is a superpower. And it starts with knowing exactly where you are and what surrounds you.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started