Linux Basics: ‘tree’ Command Usage
Hello Unixmen Readers,
In this article we will look at how to list contents in a directory as the the title implies in a tree format.
You might be thinking why will you want to list the contents of a directory with tree whilst we can use ls? I will say tree is just an advanced command based on ls.
Tree is a recursive directory listing program that produces a depth indented listing of files and directories which are colored.
First, let’s verify tree is installed in our system or not. By running:
$ tree --version
es@es-lunabox:~$ tree --version tree v1.5.3 (c) 1996 - 2009 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro
If you do not see the output it means tree is not installed, hence we need to install. tree is available in most distros default repository.
Install in Ubuntu, Elementary OS, Linux Mint, Crunchbang and other Ubuntu & Debian derivatives
$ sudo apt-get install tree
Install in Fedora and its derivatives
$ sudo yum install tree
Install in Arch Linux, Manjaro an its derivatives
$ sudo pacman -Sy tree
1. tree with no option; Running tree with no option recursively list directories and files under the current path (excluding hidden one).
2. Display hidden files and directories: By default tree doesn’t display hidden files. To display these files and directories use the -a option as used with ls.
$ tree -a
3. List directories only:
$ tree -d
4. Forward use tree on a directory:
$ tree <directory>
Run man tree for more on how to use tree.