Skip to content

The tree Command: Visualizing Directory Structures

Summary

The tree command is a powerful utility for displaying the directory structure of a system in a visually appealing, tree-like format. It provides a clear and concise overview of files and directories within a specified path.

Introduction

The tree command is not typically included in standard Linux installations, but it can easily be installed using your distribution's package manager (e.g., apt install tree on Debian/Ubuntu, yum install tree on CentOS/RHEL, brew install tree on macOS). Once installed, it allows you to recursively list the contents of directories in a hierarchical format. This can be incredibly helpful for navigating complex file systems, understanding project structures, or generating documentation.

Use Case and Examples

The primary use case for the tree command is to visualize directory structures. Here are some examples to illustrate its usage:

Basic Usage: Display the current directory structure

tree
This command, executed without any arguments, will display the directory structure of the current working directory.

Specifying a Directory: Display the structure of /home/user/documents

tree /home/user/documents
This command will display the directory structure of the /home/user/documents directory.

Limiting Depth: Display only the first level of subdirectories

tree -L 1
The -L flag specifies the maximum display depth of the directory tree. In this example, only the first level of subdirectories will be shown.

Showing Hidden Files and Directories: Display all files and directories, including those starting with a dot (.)

tree -a
The -a flag includes hidden files and directories (those starting with a dot) in the output.

Displaying File Sizes: Show the size of each file in bytes

tree -s
The -s flag displays the size of each file next to its name.

Generating an HTML file: Create a html file contain directory structure.

tree -H baseHREF -o output.html
The -H baseHREF specifies the base HREF of the HTML documents. Useful for including the output in other HTML documents. The -o output specifies the name of the html file you wish the structure to be piped to.

Commonly used flags

Flag Description Example
-a All files are listed. tree -a (Lists all files, including hidden ones)
-d List directories only. tree -d (Displays only directories, not files)
-f Print the full path prefix for each file. tree -f (Shows the full path to each file)
-L level Max display depth of the directory tree. tree -L 2 (Displays the tree up to two levels deep)
-s Print the size of each file in bytes along with the name. tree -s (Shows the size of each file)
-D Print the date of last modification or if -c is used, the date of last status change. tree -D
-h Print the size of each file but in a more human readable way, e.g. appending 'M' as in 2M. tree -h
-H baseHREF Generate HTML output with base HREF. tree -H . -o index.html (Creates an HTML file with relative paths)
-o filename Send output to file. tree -o mytree.txt (Saves the tree output to a text file)


Share on Share on

Comments