Linux tree Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the tree command in Linux to display the directory structure in a tree-like format. The lab covers the purpose and usage of the tree command, as well as exploring its basic options to customize the output. You will learn how to display file sizes, show only the directory structure, and apply the tree command to specific directories and files. The tree command is a powerful tool that helps you navigate and understand the organization of your file system more effectively.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicSystemCommandsGroup -.-> linux/tree("`Directory Tree Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/tree -.-> lab-422966{{"`Linux tree Command with Practical Examples`"}} linux/cd -.-> lab-422966{{"`Linux tree Command with Practical Examples`"}} linux/mkdir -.-> lab-422966{{"`Linux tree Command with Practical Examples`"}} linux/ls -.-> lab-422966{{"`Linux tree Command with Practical Examples`"}} linux/touch -.-> lab-422966{{"`Linux tree Command with Practical Examples`"}} linux/du -.-> lab-422966{{"`Linux tree Command with Practical Examples`"}} end

Understand the Purpose and Usage of the tree Command

In this step, you will learn about the purpose and usage of the tree command in Linux. The tree command is a powerful tool that displays the directory structure in a tree-like format, making it easier to visualize and navigate the file system.

To begin, let's install the tree command if it's not already installed in the Ubuntu 22.04 Docker container:

sudo apt-get update
sudo apt-get install -y tree

Now, let's explore the basic usage of the tree command:

tree

Example output:

.
├── project
│   └── README.md
└── .zshrc

1 directory, 2 files

The tree command, when executed without any options, displays the directory structure starting from the current working directory. It shows the directories and files in a hierarchical, tree-like format, making it easy to understand the organization of the file system.

You can also use the tree command to display the structure of a specific directory:

tree ~/project

Example output:

/home/labex/project
└── README.md

0 directories, 1 file

In this example, the tree command shows the contents of the ~/project directory.

The tree command provides various options to customize the output, such as displaying file sizes, hiding certain file types, and more. We'll explore these options in the next step.

Explore the Basic Options of the tree Command

In this step, you will explore the basic options available with the tree command to customize the output and make it more informative.

Let's start by displaying the file sizes along with the directory and file names:

tree -h

Example output:

.
├── project
│   └── README.md
└── .zshrc

1 directory, 2 files

The -h option displays the file sizes in a human-readable format (e.g., kilobytes, megabytes).

Another useful option is -d, which displays only the directory structure without the files:

tree -d

Example output:

.
└── project

You can also limit the depth of the tree output using the -L option, which specifies the maximum depth to display:

tree -L 1

Example output:

.
├── project
└── .zshrc

In this example, the -L 1 option limits the output to a depth of 1, showing only the top-level directories and files.

To exclude certain file types from the output, you can use the -I option followed by a pattern. For instance, to exclude all files with the .zsh extension:

tree -I '*.zsh'

Example output:

.
└── project
    └── README.md

The * in the pattern acts as a wildcard, matching any file with the .zsh extension.

These are just a few examples of the basic options available with the tree command. You can explore more options by running man tree to view the full list of available options and their descriptions.

Apply the tree Command to Specific Directories and Files

In this step, you will learn how to apply the tree command to specific directories and files to explore the file system structure in more detail.

Let's start by creating a sample directory structure in the ~/project directory:

mkdir -p ~/project/documents/reports
touch ~/project/documents/report1.txt
touch ~/project/documents/report2.txt
touch ~/project/documents/report3.txt

Now, let's use the tree command to display the structure of the ~/project/documents directory:

tree ~/project/documents

Example output:

/home/labex/project/documents
├── report1.txt
├── report2.txt
└── reports
    └── README.md

1 directory, 4 files

You can see that the tree command displays the directory structure, including the files and subdirectories within the ~/project/documents directory.

To display the structure of a specific file, you can use the tree command with the -f option, which shows the full path of each file:

tree -f ~/project/documents

Example output:

/home/labex/project/documents
├── /home/labex/project/documents/report1.txt
├── /home/labex/project/documents/report2.txt
└── /home/labex/project/documents/reports
    └── /home/labex/project/documents/reports/README.md

1 directory, 4 files

The -f option displays the full path of each file, making it easier to understand the location of the files within the directory structure.

You can also use the tree command to display the structure of multiple directories or files by providing them as arguments:

tree ~/project ~/Documents

Example output:

/home/labex/Documents
/home/labex/project
├── documents
│   ├── report1.txt
│   ├── report2.txt
│   └── reports
│       └── README.md
└── README.md

2 directories, 5 files

In this example, the tree command displays the structure of both the ~/project and ~/Documents directories.

These are just a few examples of how you can use the tree command to explore specific directories and files in your Linux file system. Feel free to experiment with different options and scenarios to become more familiar with this powerful tool.

Summary

In this lab, you learned about the purpose and basic usage of the tree command in Linux. The tree command is a powerful tool that displays the directory structure in a tree-like format, making it easier to visualize and navigate the file system. You explored how to install the tree command and use it to display the contents of the current working directory or a specific directory. Additionally, you learned about various options, such as displaying file sizes and showing only the directory structure without the files, to customize the output of the tree command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like