Product Reviews

How to Use tree to Show a Directory Tree in the Linux Terminal

With the tree command on Linux, you can view your entire filesystem in a hierarchical tree-like representation. Here’s how to do it.

While you may use ls in the Linux terminal to display directories, what if there was a tool that could show the directory listings hierarchically, like a tree? Fortunately, such a program does exist, and it’s called “tree.”

What Is tree?

tree is similar to the ls command in that it displays directory listings, but tree displays them as a tree-like structure, true to its name. This means that subdirectories will be represented as branches of the tree. And it’ll display a subdirectory of a subdirectory as a deeper branch.

MAKEUSEOF VIDEO OF THE DAY

Installing tree on Linux

tree is easy to install on most major Linux distributions. Just use your favorite package manager.

On Debian/Ubuntu systems:

sudo apt install tree

And on Arch Linux:

sudo pacman -S tree

On the Red Hat family of distros, including Fedora, CentOS, Rocky Linux, etc.:

sudo dnf install tree

Displaying Directory Trees With tree

Actually displaying trees with the tree command is simple. Simply calling tree in the current directory will show a tree of the directory.

You can also supply a pathname to tree as follows:

tree /path/to-directory

You can just have tree display directories with the -d option:

tree -d /etc

You can also have tree follow symbolic links on the system with the -l option. Otherwise, it’ll display symbolic links with the “link -> target” format.

If your Linux system is installed on more than one hard drive, you can have tree stay on the current filesystem with the -x option.

You can show only files that match a wildcard pattern with the -P option, followed by the pattern. Be sure to enclose the pattern in single quotes (‘), or the shell will try to interpret it and give an error.

For example, to show all files ending with the “.c” file extension:

tree -P '*.c'

Now You Can Display File Trees in Your Linux Terminal

With tree, you can display tree-like diagrams of your directories to show the relationships of files and subdirectories, and even use options to tweak the output. If you’re looking to find files in your Linux directory tree, the find command is what you need.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button