[SOLVED] bash: tree command not found in CentOS / Linux / Docker

Troubleshooting tree command not found in CentOS / Linux / Docker

When you try to use the tree command to get directory structure graphically, you get the error Message:

bash: tree: command not found

Error: bash: tree command not found

SOLUTION: Install the tree tool

If you get an error bash: tree: order not found in your Linux box (or in containers), there is a way to install it, provided you have root access

The solution: Installing tree command in CentOS using Yum repository

If you can install the tree command, follow the below-given steps. If you can’t install tree, such as lightweight containers such as Docker, there are alternative methods where you can use other Linux commands close to tree command output. -> Tree command alternatives.

# Linux / RHEL / CentOS based machine or containers:
sudo yum install tree

# Ubuntu based machine or container:
sudo apt-get install tree

Demonstrating the yum install tree command

[root@codetryout ~]# yum install tree
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: centos.xxxx
* extras: centos.xxxx
* updates: centos.xxxxxx
base                                                                                                              | 3.6 kB  00:00:00
extras                                                                                                            | 2.9 kB  00:00:00
updates                                                                                                           | 2.9 kB  00:00:00
(1/4): extras/7/x86_64/primary_db                                                                                 | 153 kB  00:00:00
(2/4): base/7/x86_64/group_gz                                                                                     | 165 kB  00:00:00
(3/4): updates/7/x86_64/primary_db                                                                                | 2.8 MB  00:00:02
(4/4): base/7/x86_64/primary_db                                                                                   | 6.0 MB  00:00:06
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================
Package                       Arch                            Version                               Repository                     Size
=========================================================================================================================================
Installing:
tree                          x86_64                          1.6.0-10.el7                          base                           46 k

Transaction Summary
=========================================================================================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Is this ok [y/d/N]:

Give Y to continue

Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                                                                                      |  46 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tree-1.6.0-10.el7.x86_64                                                                                              1/1
  Verifying  : tree-1.6.0-10.el7.x86_64                                                                                              1/1

Installed:
  tree.x86_64 0:1.6.0-10.el7

Complete!

Testing tree command with a practical example

[root@codetryout ~]# tree
.
├── anaconda-ks.cfg
├── PASSWORD.zip
├── test
│   ├── files1.txt
│   ├── files2.txt
│   ├── files3.txt
│   ├── files4.txt
│   ├── files5.txt
│   ├── files6.txt
│   ├── files7.txt
│   ├── files8.txt
│   └── files9.txt
├── tester.sh
└── zipfile.zip

1 directory, 13 files

Yum, the command will work in CentOS8, CentOS 7, CentOS 6, Linux, RHEL, Fedora Linux, and all Ubuntu flavours for complete OS or containers.

Conclusion

One of the most common errors Linux newbie users face is the tree command not found error. This article explained how to troubleshoot this error and fix it. If you have difficulty installing, such as not having sudo / root permission to do it, consider tree command alternatives.