Home Linux Commands How To View Directory Tree Structure In Linux

How To View Directory Tree Structure In Linux

By sk
Published: Last Updated on 48.9K views

Today, we are going to learn how to view directory structure using Tree command. As the name says, the tree command will display the contents of a directory in a tree-like format in Linux and Unix-like operating systems.

You might wonder why would someone use this command while we already have ls command to list the contents of a directory.

Unlike ls command, Tree command is a recursive directory listing program that produces a depth indented listing of files. It is quite useful to find the directories that contains lot of sub-directories in Unix-like systems.

Install tree in Linux

Tree command is available in the default repositories of most Linux distributions. So, it can be installed from the distribution's default package manager as shown below.

On Arch Linux and its derivatives:

$ sudo pacman -S tree

On RHEL, CentOS, Fedora:

$ sudo yum install tree

Or,

$ sudo dnf install tree

On SUSE/openSUSE:

$ sudo zypper install tree

On Debian, Linux Mint, Pop OS and Ubuntu:

$ sudo apt-get install tree

We have installed tree utility. Now, let us see some practical examples.

View Directory Tree Structure In Linux

If you run the tree command without any arguments, the tree command will display all contents of the current working directory in a tree-like format.

$ tree

Sample output:

Tree command output
Tree command output

Upon completion of listing all files/directories found, tree returns the total number of files and/or directories listed. As you see in the above output, the current directory contains 3321 sub-directories, and 40023 files.

To list the files of the specific directory in a tree-like format, say for example /etc, run:

$ tree /etc/

Sample output:

View directory Tree structure in Linux
View directory tree structure

By default, Tree will not list the hidden files. If you want to list the hidden files, use -a parameter like below.

$ tree -a /etc/

Sample output:

List the hidden files using tree command
List the hidden files using tree command

Now, check the above output. The total of number of directories and files are higher than the previous output. It is because, this time the tree command lists all directories and files including hidden files.

To view the directory structure in a colored format, use -C parameter.

$ tree -C /etc/

Sample output:

View directory structure in color
View directory structure in color

Did you notice that the tree command lists the directories and files in different colors? Yes! This is will useful to easily distinguish the directories and files.

As you may have noticed, all of the above commands lists the sub-directories and files. You can also list only the directories using -d parameter like below.

$ tree -d /etc/

Sample output:

List only directory structure and exclude sub-directories
List only directory structure and exclude sub-directories

You can also display the directory listing line by line using the following command:

$ tree -d /etc/ | less

Press ENTER to navigate through the output.

By default, Tree command will list all sub-directories and the files inside the main directory. To limit the depth or of level of recursion, use -L parameter like below.

$ tree -L 2 /etc

Here, L indicates the maximum display depth of the directory tree.

For more details, refer the man pages.

$ man tree

Conclusion

As you can see in this guide, tree command will give you a nice graphical tree view of the directory structure. You can use this command when you want to view the contents of directories that have tons of other files/folders nested inside their folders.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More