Linux Crash Course – The tree Command

In this episode of the Linux Crash Course series, you’ll learn how to use the tree command to visualize the directory structure of your Linux system. Jay shows you how to quickly display folder contents in a tree-like format, helping you better understand file organization on the command line.

YouTube player

LCC – The tree Command

Intro

Mention:

  • Hello and welcome
  • Today we’ll be covering the tree command
  • The tree command is used to view directories and subdirectories, and it shows the results in a hierarchy that makes the structure more clear
  • This is going to be a shorter video, but the goal of the LCC series is to cover as many common commands as possible, and the tree command is definitely useful

Basic usage and examples

So, let’s see the tree command in action. And this is going to be very easy, because this command is available on just about all the distributions out there. It’s often available by default, so most of you won’t need to install it. To see whether or not the command is available, we can run:

command -v tree

If you don’t see any output, then that means the tree command isn’t available. If it’s not, you should only need to install the tree package with your distributions package manager. For example, with Debian-based distrros, we can run the following command to install it:

sudo apt install tree

Anyway, with the tree command available, you’ll find that it’s very easy to use. In fact, it’s one of the easiest commands I’ve gone over in this series so far.

tree

By entering the tree command by itself, we’ll see a list of directories – but the relationship between them will be more obvious.

When you enter the tree command with no options, it’ll show you information that pertains to your current working directory. But like with many Linux commands, you can provide the tree command with a path as an option, and it’ll show you the directory layout for that directory.

tree /etc

And as useful as that example may be, the tree command becomes even more effective when we add options to the command to customize its behavior a bit. And what I’ll do right now is show you some of the highlights.

The first of these is the -s option, which will show you the size of each directory as well:

tree -s

However, I think a more useful option for viewing size is to use -h instead, which provides output that’s more human-readable.

tree -h

Next, let’s take a look at how the tree command treats hidden files. These are files or directories that begin with a period, which are going to be hidden from most commands by default (including tree). But, if you’d like to see hidden files as well, you can use the -a option:

tree -a

Another tip I’ll give you is that you can actual control how deep the directory tree is shown. This is useful for those of you that might only be interested in seeing just a few layers, rather than everything. We can customize this with the -L option, and then we provide a number that corresponds to how many directories deep we want the command to show us.

tree -L 2

In this case, we’re limiting the tree command to only show us two levels.

Continuing, here’s another option that might be useful. If you’re only interested in directories, you can limit the output to show only those:

tree -d /etc

In addition to that, we can also view permissions with the tree command as well, which we can do by adding the -p option:

tree -p /etc

Here’s another option that you might find useful. If you add the -I option (capital i) you can exclude information from the results. In my case, I have a number of *.old files in /etc, so if I’d rather not see those, I can avoid seeing them by adjusting the command like this:

tree -I “*.old” /etc

As with most commands within Linux, you can combine options together:

tree -ahp –du -L 2 /etc

In this example, I’ve combined the -a option (view hidden files) with the -h option to show human-readable filesizes, and then I added the –du option with a subdirectory limit of 2.

As you can see, the tree command can be quite helpful. Of course, there’s going to be more options available than the ones I discussed in this article, but if you consult the man page for the command you’ll see some additional options you can consider using.

Closing

  • While this was a short article, you can see, the tree command can be quite useful
  • Thanks and consider subscribing to my channel.
  • Ad-free Content
  • Early access to select videos
  • Discord access

And more!