• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

TinyGrab

Your Trusted Source for Tech, Finance & Brand Advice

  • Personal Finance
  • Tech & Social
  • Brands
  • Terms of Use
  • Privacy Policy
  • Get In Touch
  • About Us
Home » How to display only directories in Linux?

How to display only directories in Linux?

June 27, 2025 by TinyGrab Team Leave a Comment

Table of Contents

Toggle
  • Mastering the Art of Directory Listing in Linux: A Deep Dive
    • The ls -l | grep ^d Power Combo
    • Diving Deeper: Alternatives and Nuances
      • The find Command
      • The ls -F Option
      • Combining ls and awk
    • Important Considerations
    • Frequently Asked Questions (FAQs)
      • 1. How do I list only directories including hidden ones?
      • 2. How can I list directories recursively, meaning within subdirectories as well?
      • 3. How do I list directories sorted by name?
      • 4. Can I list directories modified within the last X days?
      • 5. How can I exclude a specific directory from the listing?
      • 6. I only want the directory names, not the full path. How can I achieve that with find?
      • 7. Is there a way to color-code directories differently?
      • 8. How can I list directories based on their size? (This is a tricky one!)
      • 9. Can I use wildcards with grep to filter directory names?
      • 10. How do I count the number of directories in a directory?
      • 11. What's the difference between ls -d */ and ls -F?
      • 12. Why is ls -l | grep '^d' sometimes slow on network file systems?

Mastering the Art of Directory Listing in Linux: A Deep Dive

So, you want to display only directories in Linux? It’s a common task, and the answer is refreshingly straightforward: use the ls -l command in conjunction with grep ^d. This potent combination lists files in a long format and then filters the output to display only lines that begin with the letter ‘d’ – the indicator for a directory in the long listing format. Let’s break this down and then delve into a treasure trove of related techniques and nuances.

The ls -l | grep ^d Power Combo

The magic lies in understanding what each command contributes.

  • ls -l: This command lists the contents of a directory in a long format. This format provides detailed information about each file and directory, including permissions, owner, group, size, and modification date. The very first character of each line indicates the file type. A ‘d’ indicates a directory, a ‘-‘ represents a regular file, and other characters denote symbolic links, sockets, and more.

  • grep ^d: grep is a powerful pattern-matching tool. ^ is a special character in grep that anchors the search to the beginning of the line. d is the character it’s searching for. Therefore, grep ^d searches for lines that start with the letter ‘d’.

By piping the output of ls -l to grep ^d, we’re effectively filtering the detailed listing to show only those entries that are directories. It’s a simple yet elegant solution.

Diving Deeper: Alternatives and Nuances

While ls -l | grep ^d works, there are other ways to achieve the same result, each with its own advantages and caveats.

The find Command

The find command is arguably the most versatile tool for searching for files and directories. It offers a plethora of options to filter by name, type, size, modification date, and much more. To list only directories, you can use:

find . -type d 

Here, . specifies the current directory as the starting point for the search. -type d tells find to only consider entries that are of type ‘directory’. Unlike ls -l | grep ^d, find directly targets directory entries, making it arguably more efficient. find also offers greater flexibility if you need to perform more complex filtering.

The ls -F Option

The ls -F option is a simpler alternative for visually distinguishing directories. It appends a / to the end of each directory name in the output. You can then use grep to filter for these entries:

ls -F | grep '/$' 

The $ in the grep pattern anchors the search to the end of the line. While less robust than ls -l | grep ^d or find, this method can be handy for quick visual identification and filtering, especially in scripts.

Combining ls and awk

For those comfortable with awk, you can leverage its pattern-matching capabilities to achieve the desired outcome:

ls -l | awk '/^d/ {print $NF}' 

This command uses ls -l to get the long listing, then awk filters lines that begin with ‘d’ (/^d/) and prints the last field ({print $NF}), which typically corresponds to the directory name. awk provides more control over the output format compared to simply using grep.

Important Considerations

  • Hidden Directories: By default, ls and find will not include hidden directories (directories whose names start with a dot .). To include them, use the -a option with ls or adjust the find command accordingly (e.g., find . -type d -print).

  • Pathnames: The find command displays the full pathnames relative to the starting directory. The ls command generally lists only the directory names themselves.

  • Performance: For very large directories, find can be significantly faster than ls -l

    grep ^d. This is because find directly searches for directory entries, while ls -l

Frequently Asked Questions (FAQs)

Here are some frequently asked questions about displaying directories in Linux, along with detailed answers:

1. How do I list only directories including hidden ones?

Use ls -la | grep ^d or find . -type d -print. The -a option with ls includes all entries, including hidden ones. The -print option with find ensures that results are printed to the standard output.

2. How can I list directories recursively, meaning within subdirectories as well?

The find command excels at recursion: find . -type d already searches recursively from the current directory. To specify a different starting point, replace . with the desired directory path.

3. How do I list directories sorted by name?

Pipe the output of your chosen method to sort. For example: ls -l

grep ^dsort or find . -type d

4. Can I list directories modified within the last X days?

Absolutely! find provides the -mtime option: find . -type d -mtime -7 will list directories modified within the last 7 days. -mtime +7 would list directories modified more than 7 days ago.

5. How can I exclude a specific directory from the listing?

With find, use the -not -path option: find . -type d -not -path "./excluded_directory*" will exclude any directory named "excluded_directory" and any subdirectories within it.

6. I only want the directory names, not the full path. How can I achieve that with find?

Use find . -type d -printf '%fn'. The printf action allows you to format the output. %f represents the filename without the path. n adds a newline character after each filename.

7. Is there a way to color-code directories differently?

Yes! Most Linux distributions color-code directories by default. If not, you can use ls --color=auto. You can further customize the colors using the LS_COLORS environment variable.

8. How can I list directories based on their size? (This is a tricky one!)

This isn't directly supported in a simple way because directories themselves don't have a size in the same way files do. You'd likely want to list directories and their total disk usage. du -sh */ (within a directory) will give you a summary of each directory's usage. You can then sort this output using sort -h (for human-readable sizes). This is more of an estimate than a precise size.

9. Can I use wildcards with grep to filter directory names?

Yes! For example, ls -l

grep ^d

10. How do I count the number of directories in a directory?

Pipe the output to wc -l: ls -l

grep ^dwc -l or find . -type d

11. What's the difference between ls -d */ and ls -F?

ls -d */ lists only the directories themselves, not their contents. ls -F lists all files and directories, appending a / to directories. ls -d */ is more targeted when you specifically only want the directory names.

12. Why is ls -l | grep '^d' sometimes slow on network file systems?

Listing the contents of a network file system with ls -l can be slow because it needs to fetch metadata (permissions, size, etc.) over the network for every file and directory. This is especially true for high-latency networks. The find command might be faster in some situations, but ultimately, the performance is highly dependent on the network and the file system implementation. Caching mechanisms can also play a significant role. Reducing the amount of data transferred is key to improving performance.

Filed Under: Tech & Social

Previous Post: « How to Register a Business in Pennsylvania?
Next Post: Is a home warranty tax deductible? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

NICE TO MEET YOU!

Welcome to TinyGrab! We are your trusted source of information, providing frequently asked questions (FAQs), guides, and helpful tips about technology, finance, and popular US brands. Learn more.

Copyright © 2025 · Tiny Grab