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:
[[email protected] ~]# 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:
[[email protected] ~]# 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
[[email protected] ~]# 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. Hope this helps!
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!