The tree command in Linux

Introduction

The general syntax of the command tree

tree

Installation

Installation on Rocky , CentOS , RHEL and other .rpm

sudo yum install tree

Installation on Debian , Ubuntu and other .deb

sudo apt install tree

Depth

The nesting level that tree will show can be adjusted using the flag -L

Let us look at my directory virtualbox , if you use tree without flags the result will be a display of all files and directories.

tree virtualbox

virtualbox ├── andrei@srv ├── GNUmakefile ├── iso │ └── ubuntu-20.04.3-desktop-amd64.iso ├── ubuntu │ └── GNUmakefile ├── vdi │ └── ubuntu3.vdi └── vms └── Other OS └── ubuntu3 ├── Logs │ ├── VBox.log │ ├── VBox.log.1 │ ├── VBox.log.2 │ └── VBox.log.3 ├── ubuntu3.vbox └── ubuntu3.vbox-prev 7 directories, 11 files

There are not many files in the directory, so everything fits on one screen. If you do the same with some large directory, for example, /var or /etc the output may take several minutes.

To see only the top-level files and directories you need to specify -L 1

tree -L 1 virtualbox

virtualbox ├── andrei@srv ├── GNUmakefile ├── iso ├── ubuntu ├── vdi └── vms 4 directories, 2 files

I never do that - it is easier to use ls

But -L 2, that is the top level and its first sublevels, has to be checked often. This way you can see not only the list of directories as in ls but also the subdirectories.

tree -L 2 virtualbox

virtualbox ├── andrei@srv ├── GNUmakefile ├── iso │ └── ubuntu-20.04.3-desktop-amd64.iso ├── ubuntu │ └── GNUmakefile ├── vdi │ └── ubuntu3.vdi └── vms └── Other OS 5 directories, 5 files

Article author: Andrei Olegovich

Banner image