Home > File & Directory Management > tree

Guide to the tree Command: Output Directory Structure in Tree Format

`tree` command is used in Linux and Unix-like operating systems to visually display the contents of a specified directory in a hierarchical tree format. It allows you to quickly grasp files and subdirectories, making it very useful for navigating and documenting complex project structures or file systems. In this guide, learn how to install the `tree` command, its basic usage, and various options.

Installing tree

`tree` command may not be pre-installed on most Linux distributions. Below are installation methods for major operating systems.

Install tree on Linux

You can install `tree` using the package manager of each distribution.

Install tree on Debian/Ubuntu

sudo apt update
sudo apt install tree

Use the APT package manager to install `tree`.

Install tree on Fedora/CentOS/RHEL

sudo yum install tree  # CentOS/RHEL 7 and below
sudo dnf install tree  # Fedora/CentOS/RHEL 8 and above

Use YUM or DNF package manager to install `tree`.

Overview of tree

`tree` command visually clarifies the relationships between directories and files. Unlike the `ls -R` command, which simply lists all files and directories, it uses indentation to intuitively represent the hierarchy.

Main Roles of tree

`tree` command is primarily used for the following purposes:

Key Use Cases

  • Understanding Directory Structure: Quickly comprehend the overall structure of complex projects or file systems.
  • Documentation: Save the directory structure as a text file for documentation purposes.
  • Finding Files: Visually check where specific files or directories are located in the hierarchy.
  • Troubleshooting: Useful for finding misplaced files or unexpected directories.

Main tree Command Options

`tree` command offers various options to flexibly control the output content, depth, filtering, formatting, and more.

1. Controlling Output Content

2. Filtering and Sorting

3. Output Formats and Others

Generated command:

Try combining the commands.

Description:

`tree` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to visually check the directory structure and extract necessary information through various examples of `tree` command usage.

Output Default Tree Structure of Current Directory

tree

Shows the current directory and all its subdirectories and files in tree format.

Output Only Up to 2 Levels Deep for a Specific Directory

tree -L 2 /var/log

Shows the contents of the `/var/log` directory only up to 2 levels deep to prevent excessive output.

Output Including Hidden Files/Directories

tree -a

Shows the tree structure of the current directory, including hidden items like `.git` folders or `.bashrc`.

Show File Size (Human-Readable) and Modification Time

tree -hD

Displays the size (e.g., 12K, 3.5M) and last modification time of each file along with the directory structure for quick information retrieval.

Output Including Only Specific Pattern Files

tree -P "*.js|*.css"

Shows the tree structure including only files with `.js` or `.css` extensions from the current directory. (The pattern must match the full path)

Output Excluding Specific Directory

tree -I "node_modules|\.git"

Shows the tree structure of the current directory excluding `node_modules` and `.git` directories. (Multiple patterns can be specified using a pipe `|`)

Save Tree Structure to a File

tree -o directory_structure.txt

Saves the tree structure of the current directory to a file named `directory_structure.txt`. Useful for documentation or sharing.


Same category commands