1. Introduction

In this tutorial, we’ll discuss different ways to display a directory structure’s contents recursively in Linux. We’ll first look at a couple of built-in commands, and then we’ll explore the Linux tree command and the options it provides for printing a directory structure as a tree.

2. Using ls and find

Firstly, let’s explore some built-in commands. The first one is ls -l -R (recursive), let’s have a look:

$ ls -lR
.:
total 4
drwxr-xr-x 10 ubuntu ubuntu 4096 Apr 29 18:21 ubuntu

./ubuntu:
total 12
drwxrwxr-x 3 ubuntu ubuntu 4096 Apr 29 10:30 mydirectory1
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 29 10:43 mymovies
drwxrwxr-x 4 ubuntu ubuntu 4096 Apr 29 16:28 mytrips

./ubuntu/mydirectory1:
total 4
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file1
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file2
-rw-rw-r-- 1 ubuntu ubuntu    0 Apr 29 10:30 file3
...

And the second option is using find:

$ find
.
./ubuntu
./ubuntu/.bash_history
./ubuntu/.lesshst
./ubuntu/.config
./ubuntu/.config/mc
./ubuntu/.config/mc/panels.ini
./ubuntu/.config/mc/ini
./ubuntu/.bash_logout
...

The output from these commands is less visual, but depending on our requirements, they’ll provide us with the same information.

3. Installing the tree Command

The tree command is a Linux program that lists our directories and files in a more helpful way resembling a tree structure.

Since tree is not installed by default, we’ll need to install it with apt-get or yum.

If we’re using Debian or Ubuntu, we can install tree using apt:

$ sudo apt install tree

If we’re using Red Hat, CentOS, or Fedora, we can use yum:

$ sudo yum install tree

4. Using the tree Command

4.1. Default Behavior

Once we’ve installed the tree command, we can list our directories and files in a tree format.

Executed with no arguments, it’ll print the structure of our current directory, including all subdirectories and files that are not hidden:

$ tree
.
└── ubuntu
    ├── mydirectory1
    │   ├── file1
    │   ├── file2
    │   ├── file3
    │   ├── file4
    │   └── letters
    │       ├── books
    │       ├── magazines
    │       └── readme
    ├── mymovies
    │   ├── comedy
    │   ├── drama
    │   ├── terror
    │   └── thriller
    └── mytrips
        ├── mydirexample
        │   └── file.txt
        └── pictures
            ├── Australia
            ├── France
            ├── Germany
            ├── Japan
            └── USA

7 directories, 17 files

We can clearly see a tree structure of all the directories, subdirectories, and files, connected by lines to improve visualization.

Let’s have a look at the most common flags we can use with the tree command.

4.2. Include Hidden Files and Directories

To include hidden directories and files in the output, we’ll use the -a (all) flag:

$ tree -a
.
└── ubuntu
    ├── .bash_history
    ├── .bash_logout
    ├── .bashrc
    ├── .cache
    │   ├── mc
    │   │   └── Tree
    │   └── motd.legal-displayed
    ├── .config
    │   └── mc
    │       ├── ini
    │       └── panels.ini
    ├── .gnupg
    │   └── private-keys-v1.d
    ├── .lesshst
    ├── .local
    │   └── share
    │       └── mc
    │           └── history
    ├── .profile
    ├── .ssh
    │   ├── DETELEME
    │   └── authorized_keys
    ├── .sudo_as_admin_successful
    ├── .viminfo
    ├── mydirectory1
    │   ├── file1
    │   ├── file2
    │   └── letters
...

4.3. Print Directories Only

In case we’re interested in displaying only directories, we can use the -d (directories) flag:

$ tree -d
.
├── mydirectory1
│   └── letters
├── mymovies
└── mytrips
    ├── mydirexample
    └── pictures

4.4. Include Paths

We can also display just the directories by using the -df (directories and full path) flag. We’ll see that it’s the same as -d, but the output will show us the relative path, from the current directory, of each directory that it finds:

$ tree -df
.
└── ./ubuntu
    ├── ./ubuntu/mydirectory1
    │   └── ./ubuntu/mydirectory1/letters
    ├── ./ubuntu/mymovies
    └── ./ubuntu/mytrips
        ├── ./ubuntu/mytrips/mydirexample
        └── ./ubuntu/mytrips/pictures

If we provide the full path of the starting directory, the -df option will show the full path of each directory:

$ tree -df /home/ubuntu/mytrips
/home/ubuntu/mytrips
├── /home/ubuntu/mytrips/mydirexample
└── /home/ubuntu/mytrips/pictures

We can use only the -f (full path) flag if we also want to see the files:

$ tree -f
.
├── ./mydirectory1
│   ├── ./mydirectory1/file1
│   ├── ./mydirectory1/file2
│   ├── ./mydirectory1/file3
│   ├── ./mydirectory1/file4
│   └── ./mydirectory1/letters
│       ├── ./mydirectory1/letters/books
│       ├── ./mydirectory1/letters/magazines
│       └── ./mydirectory1/letters/readme
├── ./mymovies
│   ├── ./mymovies/comedy
│   ├── ./mymovies/drama
...

4.5. Limit Number of Levels to Print

Imagine that we have a structure of hundreds of directories and subdirectories, but we only want to display until the second level of our subdirectories. We can define how many levels to display by using the -L (level) flag followed by the number of levels to visualize:

$ tree -L 2
.
└── ubuntu
    ├── mydirectory1
    ├── mymovies
    └── mytrips

4.6. Print Permissions and Sizes

Now we’re interested to see the information about file permissions and space used. We can do this using the -p (permissions) and -h (human) flags, respectively:

$ tree -ph
.
└── [drwxr-xr-x 20.0K]  ubuntu
    ├── [drwxrwxr-x 50.0K]  mydirectory1
    │   ├── [-rw-rw-r--    25.0K]  file1.txt
    │   ├── [-rw-rw-r--    75.0K]  file2.txt
    │   ├── [-rw-rw-r--    10.0K]  file3.txt
    │   ├── [-rw-rw-r--    0]  file4.txt
    │   └── [drwxrwxr-x 4.0K]  letters
    │       ├── [-rw-rw-r--   13]  books
    │       ├── [-rw-rw-r--    0]  magazines
    │       └── [-rw-rw-r--    0]  readme
    ├── [drwxrwxr-x 13.0K]  mymovies
    │   ├── [-rw-rw-r--    0]  comedy
    │   ├── [-rw-rw-r--    6.0K]  drama
    │   ├── [-rw-rw-r--    100M]  terror
    │   └── [-rw-rw-r--    0]  thriller
    └── [drwxrwxr-x 22.0K]  mytrips
        ├── [drwxrwxr-x 4.0K]  mydirexample
        │   └── [-rw-rw-r--    20M]  file.txt
        └── [drwxrwxr-x 16.0K]  pictures
            ├── [-rw-rw-r--    0]  Australia
            ├── [-rw-rw-r--    1.0M]  France
            ├── [-rw-rw-r--    5.0K]  Germany
            ├── [-rw-rw-r--    0]  Japan
            └── [-rw-rw-r--    11.0M]  USA

As we can see, it shows the same data as ls -lah but in a tree format.

4.7. Print Tree in JSON Format

The last option in this lesson is to print our directories and files in JSON (JavaScript Object Notation) format using the -J flag. This format is preferred for data exchange or if we need to process command output:

$ tree -J
[{"type":"directory","name": ".","contents":[
    {"type":"directory","name":"ubuntu","contents":[
      {"type":"directory","name":"mydirectory1","contents":[
        {"type":"file","name":"file1"},
        {"type":"file","name":"file2"},
        {"type":"file","name":"file3"},
        {"type":"file","name":"file4"},
        {"type":"directory","name":"letters","contents":[
          {"type":"file","name":"books"},
          {"type":"file","name":"magazines"},
          {"type":"file","name":"readme"}
        ]}
      ]}
    ]}
...
}]

If we want to review all the possibilities of the tree command, we can type man tree in our terminal and explore all the options.

5. Conclusion

In this article, we learned how to list our directories and files in a visually appealing format using the tree command with different flags. We also described two useful alternatives that provide the same information in a simpler format.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments