Inscrivez-vous maintenant pour un meilleur devis personnalisé!

Why I use the Linux tree command daily - and what it can do for you

May, 28, 2024 Hi-network.com
A photo of a bonzai tree.
Jack Wallen/

No matter how good the Linux GUI is, I often resort to the commandline. Part of the reason for this is habit, but the command line is also very efficient. On top of that, when you need real power and flexibility, the command line can't be beat.

What about the file manager? It's hard to beat a good GUI for managing your files.

Also: Two tricks that make using the Linux command line a lot easier

This is Linux, so for every GUI feature, there's a command that can do the same thing. Take, for instance, the tree command.  

Most GUI file managers allow for a tree view. This lets you expand a folder to view the subfolders and files within without having to first navigate inside the root folder. I prefer this style of viewing because it saves me clicks. If you're navigating in and out of a file system throughout the day, the fewer clicks you have to make, the more time and effort you'll save.

The GNOME Files app with Tree view enabled.

If you enable Tree view in GNOME Files, you can get an idea of what the tree command offers.

Jack Wallen/

Now, imagine you're doing this from the command line, and you need to check the contents of folders. If you have several folders and subfolders you have to peek in and out of, you could be using a combination of thecdand lscommands rather inefficiently. You don't have to do that much typing and guessing -- use the tree command.

Also: New to programming? My 5 favorite Linux toolswill get you up to speed faster

The tree command is a tool for recursively listing the contents of a folder. Tree adds a dash of color, so you know what's what at a glance (note that the colors are determined by the color palette configured for your terminal window). Using tree makes it very simple for you to view the layout of directories, so you're not having tocdin and out or usingls and spending too much time typing, when you only need to locate a file.

Let me show you easily you can use tree.

How to install and use tree

What you'll need: Not every Linux distribution includes the tree command. For instance, I've found tree installed by default on Fedora but not on Ubuntu. To install tree, you'll only need a running instance of Linux and a user with sudo privileges. 

1. Install tree on a Debian/Ubuntu-based distribution

Open your terminal window and install tree with the command:

sudo apt-get install tree -y

2. Install tree on a Fedora-based distribution

To install tree on a Fedora-based distribution, open the terminal window and issue the command:

sudo dnf install tree -y

3. Install tree on an Arch-based distribution

To install tree on an Arch-based distribution, open the terminal window and issue the command:

sudo pacman -S tree

6. Get started using tree

From your terminal window, issue the command:

tree

5. Add color

If you don't see the results printed in color, add the option like so:

tree -C

You should see a tree view of the folders and files in the current working directory.

6. View a specific folder

If you issue tree in your home directory, you might see a long list of files and folders fly by. You can always scroll up to find what you're looking for, or you can view the contents of a specific folder. 

Let's say you have a vast number of files and folders in ~/Music and you want to see the contents of a specific folder (let's say Rush). For that, you could issue the command:

tree ~/Music/Rush

The tree command will only print out the hierarchy in the folder specified.

The tree command showing a folder hierarchy.

Here you see how effective the tree colors can be to help discern files from folders.

Jack Wallen/

 There are other options you can use, such as:

  • tree -d -list only directories
  • tree -f -print the full path for each file
  • tree -o NAME -send the output to a file (where NAME is the name of the file)
  • tree -Q- place all file names in double quotes
  • tree -D- print the date of the last file modification

You can read about all the options available for tree with the command:

man tree

Tree makes viewing files and folders on Linux very easy, even without a GUI. I use this tool regularly and highly recommend it as a way to make your work a bit more efficient and your files/folders easier to view within your drive's hierarchy.

tag-icon Tags chauds: technologie Services et logiciels Systèmes d’exploitation

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.