Javatpoint Logo
Javatpoint Logo

Linux Tree Command

In Linux, a tree is defined as a recursive directory listing program which generates a depth-indented file's list. Without any parameters, the tree lists the files in the present directory. At the time directory parameters are given, the tree alternately lists each file per directory found in the given directories. When the list of each directory and file is complete, then the tree will return the total number of directories and files listed. Various options are available in order to modify the character which is used in the output and to use color.

Syntax

Options:

Tags Description
--help Outputs a verbose usage list.
--version Outputs the tree's version.
-a All the files are printed. By default, hidden files are not printed by trees (means files that start with a dot '.'). Under no circumstances does the tree print the file system constructs '.' (Current directory) and '..' (previous directory).
-d The -d option is used to list the directories.
-f For each file, print the full path prefix.
-i -i option creates a tree nevertheless does not print the indentation lines, if we use this along with the -f option then it is helpful.
-l Follow symbolic links to the directories as if they were directories. Those links are avoided that will result in a recursive loop.
-x Stay on the present file system, such as fins -xdev.
-P pattern Only list those files which match the pattern of wild-card. We should also use the option -a and consider those files which start with the '.' Dot for matching. There are various valid wildcard operators such as '?' (any single character), '*' (any zero or more characters), '[…]' (any single character listed between brackets (optional - (dash) for character range can be used: ex [A-Z]), and '[^….}' (in the bracket there is no single character is listed) and '|' splits alternate patterns.
-l pattern This option will not list the files which match the pattern of the wild-card.
-prune From the output tree prune makes empty directories, which are helpful when used in conjunction with -P or -l
-filelimit # Do not descend directories which include more than # entries.
-timefmt format Prints (implies -D) and formats the data based on the format string that uses the strftime syntax.
-noreport Omits file's and directory's report printing at the tree listing's end.
-p Print the protection of all files.
-s -s option will print the size of all files as well as name of the file.
-u Print UID # or username if there is no username, of the file.
-g If the file has no group name then Print the GID # or group name.
D This option is used to print the date of the last alteration time for the file listed.
-inodes Print the inode number of the directory or file.
-device Print the device number that contains the directory or file.
-F Append a '/' for directories, a '=' for socket files. A '*' for executable files and a '|' for FIFO's, as per ls -F
-q This option prints the non-printable characters inside the name of the file, like question marks rather than the default carrot notation.
-N This option prints the non-printable characters as is rather than of the default carrot notation
-r This option will sort the result in the reverse alphabetical order.
-t Sort the result according to the time of last alteration rather than alphabetical order.
dirsdirst Before files, list directories.
-n Always turn off the colorization that is over-ridden by the option -C.
-C If the variable of LS_COLORS environment is not set, always turn on colorization using the created color default. It is helpful to colorize output to the pipe.
-A While printing the indentation lines turn on ANSI line graphics hack at the time.
-S Turn on ANSII line graphics (It is helpful when we use Linux console mode fronts). Now this option is equal to '-charset=IBM437' and will ultimately be denigrated.
-L level Maximum display depth of the director tree.
-R Recursively cross down the tree each level directories (check the option -L), and at each of them execute tree again adding '-o 00Tree.html' like new option.
-H baseHREF The -H option is responsible to turn on the output of HTML, which contain HTTP references. It is helpful for ftp sites. baseHREF provides the base ftp location if using the HTML output. Means the local directory can be '/local/ftp/pub', but it should be referenced as 'ftp://hostname.oganization.domain/pub' (baseHREF must be 'ftp://hostname.organize.domain'). Hint: with this option, do not use ANSI lines, and in this directory list, do not give more than one directory. We can use colors via CSS stylesheet, use option -C besides this option to color output.
-T title In the HTML output mode, sets the title and H1 header string.
-charset charset Using this option, set the character set to be used when using HTML and for line drawings.
-nolinks In the HTML output -nolinks is used to turn off the hyperlinks.
-o file name This option sends the output to the file name.

Install Tree Command

We have to install the tree command because, by default, it is not installed. In order to install the tree command on RHEL/CentOS/Fedora Linux, we have to type the following command on the terminal:

If we want to install the tree command for Debian / Ubuntu Linux / Mint, then we have to type the following command:

If we want to install tree command for Apple OS X, type:

How to Use Tree Command

A Tree is a program that exists for Unix and Unix-like systems. It is a recursive directory listing program which generates the depth-indented listing of files.

Without any parameters, tree lists the files in the present directory. When the directory parameters are given, tree lists each and every files and directories found in the given directory subsequently. When the listing of files and directories are completed, then the tree returns the total number of directories and files listed.

Let's see some examples of the tree command that will help you to understand the tree command:

Basic Tree Output

The most general way of using the tree command:


Linux Tree Command

When we type the above command on the terminal, then the output displays the tree structure of our current directory, showing all the files, folders, and sub-folders.

Display Contents of a Specific Directory

If we want to list the files and subfolders of the particular directory instead of that of the present directory, then we are able to determine the path or directory name via the following syntax:

Example:

If we want to list all the files and sub-folders in the directory named pictures, then we have to write the following command:


Linux Tree Command

Display Hidden Files along with Other Files Using Tree

In the tree command, the list of hidden folders and files does not display in ubuntu. Although, we can use the 'a' flag in order to list them as follows.


Linux Tree Command

In the tree, the folders and files which are starting from a '.' are the hidden folders and files.

Display Only Directory Listing Through Tree

We can use 'd' flag in order to see the listing of directory but not the underlying files. In order to display only directory listing through tree we have to type the following command on the terminal.

Syntax:


Linux Tree Command

Display Full Path Prefix of Files and Folders Using Tree

Using 'f' flag, we can customize the tree flag in order to show the full path as prefix for every folders and files list.

Syntax:


Linux Tree Command

It is mainly useful when we need to know what exists where.

Display Size of Files and Folders Using Tree

Using flag 's' tree command, can print the size in terms of bytes of all the folders and files in the directory.

Syntax:


Linux Tree Command

With the help of the 's' flag, we can check which memory items consume more space on our system and getting rid of needless ones.

Display Read-Write Permission of Files and Folders Using Tree

If we want to see the delete, read and write permissions on the listed folders and files, then we can use the 'p' flag.

Syntax:


Linux Tree Command

Thus, before performing operation on a file and folder, we can first see and edit the permissions that exist on a particular item.

List Folder Contents till a Certain Level/depth Though Tree

Rather than listing each content of our directory, we are able to configure the tree command in order to show the tree to a specific level or path. Such as in tree command the level 1 can only display the list of the given folder instead of any of its subfolder.

Syntax:

Example

This command will only show the sub-directories (using -d flag) of the present directory not the additional expanded tree.

Linux Tree Command

Tree Help

The tree command is a lot more useful than what we have defined. We are able to find the command again with the tree command as follows:


Linux Tree Command
Next TopicWine Linux





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA