X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

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

If you need to see your filesystem hierarchically on display from the command line, tree is what you need. Here's how it works.
Written by Jack Wallen, Contributing Writer
A photo of a bonzai tree.
Jack Wallen/ZDNET

No matter how good the Linux GUI is, I often resort to the command line. 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/ZDNET

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 the cd and ls commands 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 tools will 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 to cd in and out or using ls 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/ZDNET

 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.

Editorial standards