Getting Started with the tree Command in Bash

Published: 2022 November 06

Last edited: 2023 September 25

system administration Linux Bash shell command software development

Introduction

The tree command is extremely useful.

According to the tree homepage:

Tree is a recursive directory listing command that produces a depth indented listing of files

In other words, tree displays a visual tree of the directory, it’s subdirectories, and all their content.

It is much faster and much more understandable than using cd and ls commands repeatedly to understand a directory structure and content.

It might best be understood by example.

Example Setup: Creating a test directory

Before we start, you will want to first create a new directory in which to test these commands.

This will make it easier to see the effects of the changes we’re making and will reduce the risk of impacting files we don’t want to alter.

It’s particularly important for this example, because we’re going to automatically create a sizable number of directories and files. Having these in a distinct test folder will make it easier to safely remove them once we’re finished with testing.

Example Setup: Creating the directories and files

To see the utility of the tree command, let’s look at an example of a large directory structure that includes several levels of nested subdirectories.


In a suitable test directory, we can use the following command to create 50 nested directories.

mkdir -p test-directory/{another/{also,and},directory1/{1,2,3,4,5},dir-a/{asparagus/{apollo,atlas},avocado,artichoke,arugula/astro,apricot/{alfred,apache},apple/{akane,ambrosia}},dir-b/{banana/{bluggoe,benedetta},blackberry,blueberry/{bluecrop,bluegold,blueray,brightwell},basil/boxwood},dir-c/{cabbage,cashew,cherry/{cristobalina,carmen,celeste,cristalina},chickpea,corn/{challenger,camelot}},other/etc}

(Thanks to markontech for demonstrating this technique.)


We can then use the following command to populate those new example directories with 153 files.

find . -type d -exec touch {}/file_{1..3}.txt \;

(Thanks to this StackOverflow answer for sharing this technique.)

Note on installing the tree command

Tree doesn’t come pre-installed on many operating systems, but it’s available on GitLab, GitHub, and in package managers for many Linux distributions.

Example 1: Viewing the files and subdirectories of a directory

Now, if you wanted to get a sense for how the directories and subdirectories were arranged and where files were located within them, how would you do it?

We could use the ls command.

Command

ls

Result

file_1.txt  file_2.txt  file_3.txt  test-directory

But in this example, you’d have to run the command 50 times, navigating into and out of each subdirectory between each run, which would take a long time.

And at the end of it, you’d be left trying to remember how the subdirectories were nested.


Alternatively, you could run the tree command and get your answer instantly.

Command

tree

Result

.
├── file_1.txt
├── file_2.txt
├── file_3.txt
└── test-directory
    ├── another
    │   ├── also
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── and
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── file_1.txt
    │   ├── file_2.txt
    │   └── file_3.txt
    ├── dir-a
    │   ├── apple
    │   │   ├── akane
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── ambrosia
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── apricot
    │   │   ├── alfred
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── apache
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── artichoke
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── arugula
    │   │   ├── astro
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── asparagus
    │   │   ├── apollo
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── atlas
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── avocado
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── file_1.txt
    │   ├── file_2.txt
    │   └── file_3.txt
    ├── dir-b
    │   ├── banana
    │   │   ├── benedetta
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── bluggoe
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── basil
    │   │   ├── boxwood
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── blackberry
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── blueberry
    │   │   ├── bluecrop
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── bluegold
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── blueray
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── brightwell
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── file_1.txt
    │   ├── file_2.txt
    │   └── file_3.txt
    ├── dir-c
    │   ├── cabbage
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── cashew
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── cherry
    │   │   ├── carmen
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── celeste
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── cristalina
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── cristobalina
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── chickpea
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── corn
    │   │   ├── camelot
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── challenger
    │   │   │   ├── file_1.txt
    │   │   │   ├── file_2.txt
    │   │   │   └── file_3.txt
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── file_1.txt
    │   ├── file_2.txt
    │   └── file_3.txt
    ├── directory1
    │   ├── 1
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── 2
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── 3
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── 4
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── 5
    │   │   ├── file_1.txt
    │   │   ├── file_2.txt
    │   │   └── file_3.txt
    │   ├── file_1.txt
    │   ├── file_2.txt
    │   └── file_3.txt
    ├── file_1.txt
    ├── file_2.txt
    ├── file_3.txt
    └── other
        ├── etc
        │   ├── file_1.txt
        │   ├── file_2.txt
        │   └── file_3.txt
        ├── file_1.txt
        ├── file_2.txt
        └── file_3.txt

50 directories, 153 files

This visual representation makes the structure and contents immediately clear.

Example 2: Viewing only the subdirectories of a directory

The tree command has several options that make it even more powerful.

For starters, the -d option limits results to just directories.

Command

tree -d

Result

.
└── test-directory
    ├── another
    │   ├── also
    │   └── and
    ├── dir-a
    │   ├── apple
    │   │   ├── akane
    │   │   └── ambrosia
    │   ├── apricot
    │   │   ├── alfred
    │   │   └── apache
    │   ├── artichoke
    │   ├── arugula
    │   │   └── astro
    │   ├── asparagus
    │   │   ├── apollo
    │   │   └── atlas
    │   └── avocado
    ├── dir-b
    │   ├── banana
    │   │   ├── benedetta
    │   │   └── bluggoe
    │   ├── basil
    │   │   └── boxwood
    │   ├── blackberry
    │   └── blueberry
    │       ├── bluecrop
    │       ├── bluegold
    │       ├── blueray
    │       └── brightwell
    ├── dir-c
    │   ├── cabbage
    │   ├── cashew
    │   ├── cherry
    │   │   ├── carmen
    │   │   ├── celeste
    │   │   ├── cristalina
    │   │   └── cristobalina
    │   ├── chickpea
    │   └── corn
    │       ├── camelot
    │       └── challenger
    ├── directory1
    │   ├── 1
    │   ├── 2
    │   ├── 3
    │   ├── 4
    │   └── 5
    └── other
        └── etc

50 directories

Example 3: Viewing the files and subdirectories of a directory to a specifed depth

Alternatively, you can limit the return to a specific depth of subdirectories with the -L option.

Command

tree -L 2

Result

.
├── file_1.txt
├── file_2.txt
├── file_3.txt
└── test-directory
    ├── another
    ├── dir-a
    ├── dir-b
    ├── dir-c
    ├── directory1
    ├── file_1.txt
    ├── file_2.txt
    ├── file_3.txt
    └── other

7 directories, 6 files

Example 4: Viewing only specific subdirectories of a directory

The -a option allows you to limit the results to specific directories.

Command

tree -a test-directory/dir-a test-directory/dir-b test-directory/dir-c

Result

test-directory/dir-a
├── apple
│   ├── akane
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── ambrosia
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── apricot
│   ├── alfred
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── apache
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── artichoke
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── arugula
│   ├── astro
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── asparagus
│   ├── apollo
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── atlas
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── avocado
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── file_1.txt
├── file_2.txt
└── file_3.txt
test-directory/dir-b
├── banana
│   ├── benedetta
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── bluggoe
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── basil
│   ├── boxwood
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── blackberry
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── blueberry
│   ├── bluecrop
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── bluegold
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── blueray
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── brightwell
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── file_1.txt
├── file_2.txt
└── file_3.txt
test-directory/dir-c
├── cabbage
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── cashew
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── cherry
│   ├── carmen
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── celeste
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── cristalina
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── cristobalina
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── chickpea
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── corn
│   ├── camelot
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── challenger
│   │   ├── file_1.txt
│   │   ├── file_2.txt
│   │   └── file_3.txt
│   ├── file_1.txt
│   ├── file_2.txt
│   └── file_3.txt
├── file_1.txt
├── file_2.txt
└── file_3.txt

35 directories, 114 files

Example 5: Combining options

These commands can be combined.

For example, you can limit to specified directories, to a specified depth, and only return directories.

Command

tree -d -L 1 -a test-directory/dir-a test-directory/dir-b test-directory/dir-c

Result

test-directory/dir-a
├── apple
├── apricot
├── artichoke
├── arugula
├── asparagus
└── avocado
test-directory/dir-b
├── banana
├── basil
├── blackberry
└── blueberry
test-directory/dir-c
├── cabbage
├── cashew
├── cherry
├── chickpea
└── corn

15 directories

More options

This is just an introduction - there are additional options that can be found via the tldr page:

tldr tree

Or the man page:

man tree

Conclusion

The tree command makes it easier to explore and understand directory and file structures from the command line.

It is much faster and much more digestible than using cd and ls commands repeatedly to understand a directory structure and content.

Copying Directory Structure With a Bash Script - With Help From ChatGPT AI

Published: 2024 January 22

Setting Up a Raspberry Pi as a Linux Print Server with CUPS

Published: 2024 January 19

Setting Up a Raspberry Pi Zero 2 W with Ubuntu Server

Published: 2024 January 04