Tree Command in Linux - With Examples ?

Like Windows, Linux also has a program called the tree that allows directories and lists of files to be displayed in a depth-indented. The tree command lists current directory contents. When directory is displayed, the tree command will list the files contained in that directory in turn.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux system tools queries.

In this context, we shall look into how to use the tree command in Linux.

More about the Linux tree command

Tree is a recursive directory listing program that produces a depth indented listing of files, 

which is colorized ala dircolors if the LS_COLORS environment variable is set and output is to tty.

With no arguments, tree lists the files in the current  directory. 

When directory arguments are 

given, tree lists all the files and/or directories found in the given directories each in turn. 

Upon completion of listing all files/directories found, tree returns the total number of files 

and/or directories listed.

How to install tree command ?

You can run the below command to install the tree command:

$ sudo apt install tree

What is the syntax of the tree command ?

It is given below:

$ tree [options] [directory]

The tree command Options is explained below with examples

1. To list current directory contents, Navigate to the directory and run the command:

$ tree

2. -a list all files:

$ tree -a

3. -f print the full path:

$ tree -f

4. -d list directories only:

$ tree -d

5. -p show each file access permission:

$ tree -p

6. -u print the username:

$ tree -u

[Need help in fixing Linux system issues ? We can help you. ]

This article covers how to use the tree command in Linux. In fact, the tree command lists contents of directories in a tree-like format. 

Following is its syntax:

$ tree [OPTIONS] [directory]

How to Install Tree Command in Linux ?

The tree command is not available by default in Linux distributions, and can be installed in Debian and RedHat-based distributions with:

$ sudo apt install tree   [on Debian, Ubuntu & Mint]
$ sudo yum install tree   [on RedHat, CentOS & Fedora]
$ sudo pacman -Sy tree    [on Arch and Manjaro Linux]

To Verify if it has been installed, run the below command:

$ tree -v

Related Posts