Billboard Ads

In Linux, the ls command is used to list files and directories in the current working directory. But what if you only want to see directories and exclude the files? Can ls do that? Let’s dive into different ways to list only directories using ls and other tools.

1. Using ls with grep

You can list directories in a detailed format using ls -l, and then filter the result to only show directories with grep.

ls -l | grep ^d

Explanation:

ls -l gives a detailed list of files and directories. grep ^d filters the results to only include lines that start with a d, which denotes a directory in the output.

Example:

If you run ls -l, you'll see something like this:

drwxr-xr-x 2 user user 4096 Apr  1 09:00 folder1
-rw-r--r-- 1 user user  100 Apr  1 09:05 file1.txt
drwxr-xr-x 2 user user 4096 Apr  1 09:10 folder2

With the command ls -l | grep ^d, you’ll only get:

drwxr-xr-x 2 user user 4096 Apr  1 09:00 folder1
drwxr-xr-x 2 user user 4096 Apr  1 09:10 folder2

2. Using Shell Globbing (*/)

You can use shell globbing to list directories only, excluding files.

ls -d */

Explanation:

The */ pattern matches directories because */ is a glob pattern that ends with a /, which is the standard directory identifier.

Example:

If you run ls -d */, you'll see:

folder1/  folder2/

3. Using find Command

The find command offers a powerful way to search for directories only, even recursively.

find . -type d

Explanation:

. → Tells find to search starting from the current directory.

-type d → Filters the search to directories only.

Example:

If you run find . -type d, you’ll see:

.
./folder1
./folder2
./folder2/subfolder

This command shows all directories, including nested ones.

4. Using find with -maxdepth to Limit Depth

If you want to list directories only up to a certain depth (for example, just the top-level directories), you can use the -maxdepth option with find.

find . -maxdepth 1 -type d

 Explanation:

-maxdepth 1 limits the search to one level deep, meaning it won’t go into subdirectories.

Example:

If you run find . -maxdepth 1 -type d, you’ll only get:

.
./folder1
./folder2

5. Using tree (Optional Tool)

If you have tree installed, you can use it to list directories in a tree format, restricting it to directories only.

tree -d -L 1

Explanation:

-d restricts the output to directories only.

-L 1 limits the depth to one level.

Example:

If you run tree -d -L 1, you’ll see:

.
├── folder1
└── folder2

Comparison: ls -R vs find . -type d

Command Description Output Example
ls -R Recursively lists all files and directories Shows directories and files
find . -type d Recursively lists only directories Shows directories only

🛠️ Caution:

The ls -l | grep ^d method will only work in the current directory. It won’t display directories recursively unless you add the -R option to ls (though that includes files as well).

The find method will show all directories, including hidden ones (those starting with a .). To exclude hidden directories, you can add a ! -name '.*' filter to your find command:

find . -type d ! -name '.*'

⚙️ Alternative Commands:

  • ls -d */ — A quick way to list only directories in the current directory.
  • find . -type d — A flexible way to list directories recursively.
  • tree -d — A tool for listing directories in a tree format.

💡 Tips for Usage:

  • Combining Commands: You can chain commands to perform more complex directory listings.
  • Example: List directories and save them to a file:
    find . -type d > directories.txt
  • With Wildcards: Use * to search for directories matching specific patterns:
    ls -d */mydir*
  • Excluding Specific Directories: Use grep to exclude specific directories:
    find . -type d | grep -v "folder1"

Conclusion:

As you can see, there are several ways to list only directories in Linux. Whether you want a simple output using ls -d */, a more detailed list with ls -l | grep ^d, or a recursive directory search with find . -type d, Linux offers a variety of tools to suit your needs. Each method has its use case, so you can choose based on your requirements. Happy directory hunting! 🚀