What is the tree command and what is it for?
How to install tree?
How to use the tree command?
Today we are going to talk about a command that is not included by default in the distros that I have tried, but that many times are worth installing because it
shows us in a more graphic way the structure of the directories
. We are talking about the
tree
command, which I am sure that users with more experience already sound of using it in those wonderful years of MS-DOS.
What is the tree command and what is it for?
The tree command allows us to recursively list the directories by generating a tree-shaped list.
This tree list also contains bleeds and colors that allow a more intuitive view of the directory structure.
When we have to work with complex directory structures, the
tree
command can be of great help to display all hierarchies on the screen.
How to install tree?
If the distro does not include tree by default, as with Ubuntu, we can install it with
sudo apt-get install tree
and if we are in CentOS we use the
yum install tree
command.
Once installed, we can access the user manual
with the
man tree
command, which I recommend using whenever there is any doubt.
Comment that sometimes the
tree
command did not show me the different colors, but this was because the environment variable
LS_COLORS
was not set. Rarely has this happened to me, but I leave it in case someone happens to know how to fix it.
How to use the tree command?
Obviously,
we will use the command through the terminal
and if we execute it without any argument,
tree
list the contents of the current directory. As
tree
has great flexibility, I will show some examples of use to publicize its possibilities.
Examples of using the tree command:
-
With the
tree -d
command, only directories are listed, completely ignoring files of any extension.
-
With
tree -a
a list is made of all files including hidden files.
-
If we use
tree -ugh
, the owner (
u
), the group (
g
) and the file size (
h
) are shown.
-
With
tree -H ./ -o arbol.html -T "Titulo h1 del documento HTML"
export the tree to an HTML file and also add a title.
-
With
tree -P *.js
we would be using the
*.js
pattern that shows us only javascript files with the extension
.js
. To achieve the opposite effect, that is, list all files except those with the extension
.js
, we would use the
tree -I *.js
command.