7 Easy Methods to List Only Directories Linux 

Written by

Reviewed by

Last updated: June 25, 2024

Expert verified

SVG Image

TL;DR

To list only directories Linux, you can try these methods:

  1. Use ls -d */ to quickly list directories in the current location, excluding files or subdirectories.
  2. Utilize find -type d to search and list directories within the current directory and its subdirectories.
  3. Display a tree-like structure of directories only using tree -d, providing a clear view of the hierarchy.
  4. List directories with their sizes by executing du -h --max-depth=1 | grep -E '^[0-9.]+[GMK]?\s+./' in the terminal.

Discover different methods of how to list only directories in Linux and best practices for listing them in the guide below.

Having trouble keeping track of directories in Linux? Don’t worry, I’ve got you covered! In this post, I’ll walk you through simple, step-by-step methods to list only directories Linux. You’ll explore powerful commands like ls, find, tree, and du, and I’ll share best practices to keep your system organized and efficient. By the end of this guide, you’ll be managing your directories like a pro. Let’s make directory management easier for you!

Importance of Directory Listing in System Administration

Directory listing is a crucial task in system administration. Here’s why it matters:

  • Organize Files and Directories: Directory listing helps system administrators see the structure and contents of the file system. This makes it easier to find, manage, and organize files and directories efficiently.
  • Monitor System Changes: By regularly listing directories, admins can track changes in the file system. This helps in spotting new directories, unauthorized changes, and ensuring everything is correctly placed.
  • Manage Disk Space: Knowing the directory structure helps in managing disk space. Admins can identify large directories and clean up unnecessary files to free up space.
  • Security and Permissions: Directory listing allows admins to check and set proper permissions on directories. This ensures that only authorized users can access sensitive information.
  • Backup and Recovery: Understanding the directory structure is essential for creating effective backups. It ensures that all important directories are included and can be restored accurately if needed.
  • Automation and Scripting: Accurate directory listings are necessary for writing scripts that move, copy, or manipulate files and directories. This helps in automating various administrative tasks efficiently.

How to List Only Directories Linux

To list only directories Linux, you can use several commands. The ls -d */ command lists directories in the current location. For more advanced searches, use find /path -type d to recursively list directories within a specified path. The tree -d command provides a tree-like view of directories, and du -h --max-depth=1 | grep -E '^[0-9.]+[GMK]?\s+\.\/' lists directories along with their sizes.

Here are the detailed steps for seven different methods to Linux list directories only:

1. ls Command with the -d Option

The ls command is a versatile tool in Linux that allows you to list files and directories. By using the -d option, you can modify its behavior to display only directories. This method is perfect when you want a quick overview of the directories within your current location, excluding any files or subdirectories. Here’s how to ls only directories:

  1. Open the Terminal.
opening terminal 22
  1. Type the following command to ls list only directories:
ls -d */
  1. Press Enter to execute the command. The command will list all the directories within the current directory, excluding any files or subdirectories.
listing all directories excluding subdirectories

2. find Command

The find command is a powerful tool designed for searching files and directories based on various criteria. When it comes to listing directories, you can leverage the find command by specifying the -type d option. This method enables you to search for directories within the current directory and its subdirectories. Here’s how to use it to list directories:

  1. Open the command prompt and type the following command:
find -type d
  1. This command instructs the find command to search for directories (-type d) within the current directory and its subdirectories.
listing directories subdirectories and their files

3. Tree Command

The tree command provides a visual representation of the directory structure. It will display a tree-like structure with only directories, omitting files. It can be a helpful tool to grasp the hierarchy and relationships between directories quickly. To Linux list directories using a tree, follow these steps:

  1. Launch your Terminal window and execute the following command:
tree -d
  1. The tree command will display a tree-like structure with only directories, omitting files.
viewing directories in a tree format

4. du Command

The du command is primarily used to determine the disk usage of files and directories. However, it can also be employed to list directories along with their sizes. To only list directories Linux, follow these steps:

  1. Access your command window and enter the following command:
du -h --max-depth=1 | grep -E '^[0-9.]+[GMK]?\s+\.\/'

This command uses du to display the sizes of directories (-h for human-readable format) within the current directory.

The --max-depth=1 flag limits the listing to the immediate subdirectories, and grep filters the output to show only directories.

The result will be a list of directories with their corresponding sizes.

    listing directories with their corresponding sizes

    5. stat Command with awk

    The stat command provides detailed information about files and directories. When combined with awk, it can filter out directories.

    1. Access your command window.
    1. Run the following command:
    for item in *; do stat "$item" | awk '/directory/ {print $0}'; done
    • for item in *: Iterates over each item in the current directory.
    • stat “$item”: Retrieves information about the item.
    • awk ‘/directory/ {print $0}’: Filters and prints only directories.
    listing all available directories with details using awk

    6. Bash Built-in [[ -d ]]

    This method uses a simple bash loop to check if an item is a directory using the [[ -d ]] test.

    1. Launch your Terminal window.
    1. Execute the following command:
    for item in *; do [[ -d $item ]] && echo "$item"; done
    • for item in *: Iterates over each item in the current directory.
    • [[ -d $item ]]: Checks if the item is a directory.
    • echo “$item”: Prints the directory name if the condition is true.
    listing directories using bash loop

    7. ls Command with grep

    The ls command lists all files and directories in the current directory. By combining it with grep, you can filter the results to show only directories.

    1. Open your terminal.
    1. Run the following command:
    ls -l | grep "^d"
    • ls -l: Lists files and directories with detailed information in a long format.
    • grep “^d”: Filters the output to show only lines that start with “d”, which indicates directories.
    listing directories using ls command with grep

    Best Practices for Efficient Directory Listing

    Efficient directory listing is important for system performance and organization. By following these best practices, you can make sure your directory listing tasks are quick and resource-friendly. Here are five simple tips to help you achieve efficient directory listing:

    • 📝 Use Simple Commands for Small Directories: For small directories, use basic commands like ls with appropriate filters. They are quick and require minimal system resources, making them ideal for simple tasks.
    • 🔍 Limit Search Depth: When using commands like find, use options such as -maxdepth to limit the search depth. This reduces the amount of data processed, saving time and resources.
    • 🕒 Schedule Resource-Heavy Tasks: Run intensive tasks, like recursive directory searches, during off-peak hours. This minimizes the impact on system performance and ensures other tasks run smoothly.
    • 🧹 Regularly Clean Up Directories: Keep directories organized and free of unnecessary files. Regular maintenance helps improve directory listing speed and overall system performance by reducing clutter.
    • ⚙️ Use Efficient Tools: Choose the right tool for the job. Use commands like tree for visual representations and find for complex searches. Matching the tool to the task ensures optimal performance.

    Linux List Only Directories: Final Thoughts

    In this article, I’ve shown you step-by-step methods to list only directories in Linux, using commands like ls, find, tree, and du. I’ve also shared best practices for efficient directory listing, such as using simple commands for small tasks and scheduling resource-heavy tasks.

    If you found this guide helpful, you might want to explore more related topics:

    Frequently Asked Questions

    How can I list directories recursively?

    To list directories recursively in Linux, you have a couple of options. One option is to use the -R option with the ls command. For example, you can use ls -R to list all directories recursively, including subdirectories and their contents. Another option is to utilize the -type d option with the find command. By using the command find -type d, you can recursively search for directories starting from the current directory and display the results. Both commands will traverse all subdirectories and list the directories they contain, giving you a comprehensive view of the directory structure.

    Can I display the permissions of directories while listing them?

    Yes, you can display the permissions of directories while listing them in Linux. By using the -l option with the ls command, you can obtain detailed information about the listed directories, including their permissions. When you execute the command ls -l, it will show the permissions, ownership, file size, and other metadata of the directories. The permissions are represented by a combination of letters and symbols such as r for read, w for write, and x for execute. By displaying permissions, you can quickly assess the access rights and security settings of the listed directories.

    Is it possible to sort the listed directories by size?

    Absolutely! You can sort the listed directories by size in Linux using different commands. One way is to utilize the --sort=size option with the ls command. By executing the command ls --sort=size, the directories will be listed in ascending order based on their sizes. Alternatively, you can combine the du command, which calculates disk usage, with the sort command. For instance, running du -h --max-depth=1 | sort -hr will display the directories and their sizes in human-readable format, sorted in descending order based on size. Sorting directories by size allows you to identify large directories and manage your storage efficiently.

    Are there any graphical tools available for listing directories in Linux?

    Yes, Linux provides several graphical file managers that offer a user-friendly interface for browsing and listing directories. Some popular graphical file managers include Nautilus, Dolphin, and Thunar. These tools present directories and files in a visually appealing manner, allowing you to navigate and explore your file system easily. With graphical file managers, you can simply click on directories to expand or collapse them, visually browse folder hierarchies, and perform operations like copy, move, and delete with intuitive drag-and-drop functionality. These graphical tools enhance the user experience and provide an alternative to the command-line-based directory listing, particularly for users who prefer a visual approach to file management.

    Ojash

    Author

    Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

    Akshat

    Reviewer

    Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

    Share this article
    Shareable URL
    Prev Post

    How to Install Eclipse in Ubuntu [3 Easy Methods]

    Next Post

    How to List All Services Ubuntu [4 Best Methods]

    Leave a Reply

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

    Read next