Is there an equivalent to tree on CentOS?
6 Answers
If tree is not installed on your Centos system (I typically recommend server setups to use minimal install disk anyhow) you should type the following at your command line:
# yum install tree -y
If this doesn't install it's because you don't have the proper repository. I would use the Dag Wieers repository:
http://dag.wieers.com/rpm/FAQ.php#B
After that you can do your install:
# yum install tree -y
Now you're ready to roll. Always read the man page: http://linux.die.net/man/1/tree
So quite simply the following will return a tree:
# tree
Alternatively you can output this to a text file. There's a ton of options too.. Again, read your man page if you're looking for something other than default output.
# tree > recursive_directory_list.txt
(^^ in a text file for later review ^^)

- 2,289
- 2
- 14
- 8
-
what does -y mean? – Robert Houghton Feb 05 '20 at 15:28
-
1-y means yes. Install thing and don’t prompt me to ask if I want to proceed with the installation. – Lou Grossi Feb 08 '22 at 02:50
You can make your own primitive "tree" ( for fun :) )
#!/bin/bash
# only if you have bash 4 in your CentOS system
shopt -s globstar
for file in **/*
do
slash=${file//[^\/]}
case "${#slash}" in
0) echo "|-- ${file}";;
1) echo "| |-- ${file}";;
2) echo "| | |-- ${file}";;
esac
done

- 6,665
- 1
- 17
- 14
-
Thanks man, en lieu of Carlos's answer I'll add this to my .bash_profile – nsfyn55 Apr 20 '11 at 15:40
-
-
And if you want to add color: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux – Robert Houghton Feb 05 '20 at 21:08
Since tree
is not installed by default in CentOS ...
[user@CentOS test]$ tree
-bash: tree: command not found
[user@CentOS test]$
You can also use the following ls
command to produce almost similar output with tree
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
Example:
[user@CentOS test]$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
.
|-directory1
|-directory2
|-directory3
[user@CentOS directory]$
As you can see here. tree is not installed by default in CentOs, so you'll need to look for an RPM and install it manually

- 45
- 2
- 11

- 4,438
- 1
- 23
- 26
You have tree in the base repo.
Show it (yum list package-name):
# yum list tree
Available Packages
tree.i386 1.5.0-4 base
Install it:
yum install tree
(verified on CentOS 5 and 6)

- 169
- 2
- 4
I need to work on a remote computer that won't allow me to yum
install. So I modified bash-o-logist's answer to get a more flexible one.
It takes an (optional) argument that is the maximum level of subdirectories you want to show. Add it to your $PATH
, and enjoy a tree
command that doesn't need installation.
I am not an expert in shell (I had to Google a ton of times just for this very short script). So if I did anything wrong, please let me know. Thank you so much!
#!/bin/bash
# only if you have bash 4 in your CentOS system
shopt -s globstar # enable double star
max_level=${1:-10}
for file in **
do
# Get just the folder or filename
IFS='/'
read -ra ADDR <<< "$file"
last_field=${ADDR[-1]}
IFS=' '
# Get the number of slashes
slash=${file//[^\/]}
# print folder or file with correct number of leadings
if [ ${#slash} -lt $max_level ]
then
spaces=" "
leading=""
if [ "${#slash}" -gt 0 ]
then
leading=`eval $(echo printf '"|${spaces}%0.s"' {1..${#slash}})`
fi
echo "${leading}|-- $last_field"
fi
done

- 111
- 3