
tree Command in Linux
The tree command in Linux displays the contents of a directory in a tree-like structure. It makes it easier to visualize nested files and folders. It is especially useful for quickly understanding project layouts or verifying directory contents. It also helps in visualizing directory hierarchies, which are useful in documentation, troubleshooting, and codebase overviews.
Table of Contents
Here is a comprehensive guide to the options available with the tree command in Linux β
- Installation of tree Command
- Syntax of tree Command
- tree Command Options
- Examples of tree Command in Linux
Installation of tree Command
By default, the tree command may not be installed on Linux. To install it on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command β
sudo apt install tree
To install it on Arch Linux, use the command below β
sudo pacman -S tree
To install tree on Fedora, use the following command β
sudo dnf install tree
To verify the installation of the tree command, check its version β
tree --version

Syntax of tree Command
The syntax of the tree command in Linux is as follows β
tree [options] [directory]
In the above syntax, the [options] field is used to specify the optional flags to change the commandΓ’ΒΒs operation. The [directory] argument is used to specify the directory whose content is to be displayed in the tree-like structure.
tree Command Options
The options for the Linux tree command are listed below β
Short Option | Long Option | Description |
---|---|---|
-a | Show all files, including hidden ones. | |
-d | List directories only. | |
-f | Print the full file path. | |
-i | Omit indentation lines. | |
-l | Follow directory symlinks. | |
-x | Stay on the current file system. | |
-P pattern | Include only files matching the pattern. | |
-I pattern | Exclude files matching a pattern. | |
--noreport | Skip summary report. | |
-p | Show file type and permissions. | |
-s | Show file size in bytes. | |
-h | Show file size in human-readable format. | |
-u | Show the file owner's username or UID. | |
-g | Show group name or GID. | |
-D | Show file modification date. | |
--inodes | Show inode numbers. | |
--device | Show device numbers. | |
-F | Append file type indicators. | |
-q | Show unprintable chars as ?. | |
-N | Show unprintable chars as-is. | |
-v | Sort by file version numbers. | |
-r | Reverse sort order. | |
-t | Sort by modification time. | |
--dirsfirst | List directories before files. | |
-n | Disable color output. | |
-C | Enable color output. | |
-A | Use ANSI line graphics. | |
-S | Use ASCII line graphics. | |
-L level | Limit display depth. | |
--filelimit # | Skip dirs with more than # entries. | |
-R | Recursive directory traversal. | |
-H baseHREF | Enable HTML output with base URL. | |
-T title | Set the HTML output title. | |
--charset charset | Set HTML output charset. | |
--nolinks | Disable HTML hyperlinks. | |
-o filename | Write output to file. | |
--help | Verbose usage listing. | |
--version | Show the tree version. |
Examples of tree Command in Linux
This section explores how to use the tree command in Linux with examples β
Showing the Current Directory as a Tree
To display the contents of the current directory in a tree-like structure, use the tree command without any options β
tree

Showing a Specific Directory as a Tree
To print the contents of a specific directory in a tree-like format, use the tree command followed by the directory β
tree /etc

Limiting the Display Depth
To limit display depth, use the -L option β
tree -L 2

The above sets the display depth to level 2.
Note that if the directory structure is very large, using the -L option might cut off parts of the tree, potentially hiding important files or directories.
Showing Directories Only
To print the directories only, use the -d option with the tree command β
tree -d

Including Hidden Files
By default, the tree command skips the hidden files from the output. To display the hidden files, use the -a option β
tree -a

Displaying Full Paths
To display the full paths of each file, use the -f option β
tree -f

Displaying File Size in Human-readable Format
To print the file size in a human-readable format, use the -s and -h options β
tree -s -h

Excluding the Specific Files
To exclude a specific file type, use the -I option followed by a pattern. For example, to exclude all the text files, use the tree command in the following way β
tree -I "*.txt"
Displaying the Specific Files
To include a specific file type, use the -P option followed by a pattern. For example, to show all the text files, use the tree command in the following way β
tree -P "*.txt"
Skipping Output Summary
To skip the output summary, use the --noreport option β
tree -d --noreport

Saving the Output to a File
To save the output to a file, use the -o option followed by the filename β
tree -d -o file.txt

Getting Colored Output
To display the colored output, use the -C option β
tree -C
Displaying Usage Help
To display the usage help for the tree command, use the --help option β
tree --help
Conclusion
The tree command in Linux provides a simple and visual way to display directory structures in a tree-like format. It helps in quickly understanding how files and folders are organized, making it useful for documentation, troubleshooting, and analyzing project layouts.
With various options for customizing the output, such as showing hidden files, limiting depth, displaying file sizes, or saving the result, the tree command is a flexible tool for managing and reviewing file systems efficiently.