Tree command is a useful Linux utility to list files and directories graphically, however, this is not a built-in Linux command with the majority of Linux distros, for keeping it light containers also avoid including the tree commands.
So here are some ideas to tackle this situation.
tree command not working in linux or containersSOLUTION: Use tree command alternatives, such as find or ls -lR
Tree command, example output:
[root@codetryout ~]# tree . ├── test │ ├── files1.txt │ ├── files8.txt │ └── files9.txt ├── codetryout.sh └── zipfile.zip 1 directory, 5 files
There are situations such as,
- You want to keep the number of packages to a minimum, such as a docker image/container.
- Installation of the package is not permitted, or you do not have sudo or root access
There you can use inbuilt Linux commands instead of the tree. Solutions mentioned here will not require sudo access package installation. If you prefer to install the tree command instead of alternatives, please refer to this guide: how to install tree in Ubuntu.
#1. The first alternative for the tree command: find
Use the find command. Let us see an example of the find command:
[root@codetryout ~]# find . ./test ./test/files1.txt ./test/files8.txt ./test/files9.txt ./codetryout.sh ./zipfile.zip
Just CD to the directory you want to get a tree view and execute the find command
#2. The second alternative for the tree command: ls -lR
Use the ls -lR command. Let us see an example usage of ls -lR
[root@codetryout ~]# ls -lR .: -rwxr--r--. 1 root root 0 Jun 29 18:45 codetryout.sh -rw-r--r--. 1 root root 990 May 17 17:06 zipfile.zip ./test: total 0 -rw-r--r-- 1 root root 0 Nov 2 03:10 files1.txt -rw-r--r-- 1 root root 0 Nov 2 03:10 files8.txt -rw-r--r-- 1 root root 0 Nov 2 03:10 files9.txt
Find and ls should be part of your system and no installation would be required.
Third-party packages are available as alternatives to tree command
Here are a few popular alternatives, which you need to install:
lsd (https://github.com/Peltoche/lsd): lsd is a modern, cross-platform alternative to ls with support for displaying directory trees. It provides a colorful and more intuitive representation of the directory structure.
exa (https://the.exa.website/): exa is another modern replacement for ls that offers a tree-like view of directories. It also provides additional features such as Git integration and extended file information.
These alternatives offer various features and customization options, so you can choose the one that best fits your requirements and preferences.
The tree command has a better way of displaying the file and directory hierarchy, however, if you don’t have a way to get it installed, these methods could be useful!