How to show only directories in the directory tree in Linux?

LinuxLinuxBeginner
Practice Now

Introduction

Navigating the Linux directory structure and understanding how to list files and directories are fundamental skills for any Linux user. In this tutorial, we will explore how to display only directories in the directory tree, empowering you to better manage and organize your Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/cd -.-> lab-415598{{"`How to show only directories in the directory tree in Linux?`"}} linux/pwd -.-> lab-415598{{"`How to show only directories in the directory tree in Linux?`"}} linux/mkdir -.-> lab-415598{{"`How to show only directories in the directory tree in Linux?`"}} linux/ls -.-> lab-415598{{"`How to show only directories in the directory tree in Linux?`"}} linux/wildcard -.-> lab-415598{{"`How to show only directories in the directory tree in Linux?`"}} end

Understanding Linux Directory Structure

Linux has a hierarchical directory structure, where directories (also known as folders) are used to organize files and other directories. The top-level directory is called the root directory, denoted by a forward slash (/). All other directories and files are organized under this root directory.

The Linux File System Hierarchy

The Filesystem Hierarchy Standard (FHS) defines the standard directory structure and contents in Linux systems. The main directories under the root directory are:

  • /bin: Contains essential user binary (executable) files.
  • /etc: Contains system configuration files.
  • /home: Contains user home directories.
  • /opt: Contains optional software packages.
  • /tmp: Contains temporary files.
  • /usr: Contains user-related programs and files.
  • /var: Contains variable data files, such as logs and spool files.

These directories and their subdirectories form the overall Linux directory structure, which helps organize and manage files and directories on the system.

In Linux, you can navigate the directory structure using the cd (change directory) command. For example, to change to the /home/user directory, you would use the command:

cd /home/user

You can also use relative paths to navigate between directories. For example, if you are in the /home/user directory and want to go to the /home/user/documents directory, you can use the command:

cd documents

Understanding the Linux directory structure and how to navigate it is essential for effectively managing files and directories on a Linux system.

Listing Files and Directories

In Linux, you can use the ls (list) command to display the contents of a directory. The basic syntax for the ls command is:

ls [options] [path]

where [options] are the various flags you can use to customize the output, and [path] is the directory you want to list.

Basic Usage of ls Command

To list the contents of the current directory, you can simply use the ls command:

$ ls
Documents  Downloads  Music  Pictures  Public  Templates  Videos

To list the contents of a specific directory, you can provide the path as an argument:

$ ls /home/user
Documents  Downloads  Music  Pictures  Public  Templates  Videos

Customizing ls Output

You can use various options with the ls command to customize the output. Some common options include:

  • -l: Displays the long-format listing, which includes file permissions, ownership, size, and modification time.
  • -a: Displays all files, including hidden files (files starting with a dot).
  • -h: Displays file sizes in human-readable format (e.g., KB, MB, GB).
  • -t: Sorts the output by modification time, with the most recent files first.

For example, to list the contents of the current directory in long format with human-readable file sizes, you can use the following command:

$ ls -lh
total 32K
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Documents
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Downloads
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Music
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Pictures
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Public
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Templates
drwxr-xr-x 2 user user 4.0K Apr 14 12:34 Videos

Understanding how to use the ls command and its various options is essential for navigating and managing files and directories in a Linux system.

Displaying Only Directories

While the ls command can list both files and directories, there may be times when you want to display only the directories in a given directory. You can achieve this using the following methods:

Using the -d Option with ls

The -d option with the ls command instructs it to list the directory entries themselves, rather than their contents. This means that it will only display the directories, and not any files within them.

$ ls -d */
Documents/  Downloads/  Music/  Pictures/  Public/  Templates/  Videos/

Using the -type d Option with ls

Another way to display only directories is to use the -type d option with the ls command. This option filters the output to include only directory entries.

$ ls -l -type d
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Documents
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Downloads
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Music
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Pictures
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Public
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Templates
drwxr-xr-x 2 user user 4096 Apr 14 12:34 Videos

Both of these methods allow you to display only the directories in a given directory, which can be useful when you need to focus on the directory structure rather than the individual files.

Summary

By the end of this tutorial, you will have learned how to effectively list directories in the Linux directory tree, allowing you to focus on the essential information and streamline your workflow. This knowledge will be invaluable as you continue to explore and utilize the power of the Linux operating system.

Other Linux Tutorials you may like