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

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
tree Command in Linux1

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
tree Command in Linux2

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
tree Command in Linux3

Limiting the Display Depth

To limit display depth, use the -L option βˆ’

tree -L 2
tree Command in Linux4

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
tree Command in Linux5

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
tree Command in Linux6

Displaying Full Paths

To display the full paths of each file, use the -f option βˆ’

tree -f
tree Command in Linux7

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
tree Command in Linux8

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
tree Command in Linux9

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
tree Command in Linux10

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.

Advertisements