In this article, we will discuss how to print a directory structure like a tree in Linux.
Table Of Contents
- Introduction
- Using tree command:
- Print all files and directories like tree
- Print Directories only
- Print Directories only like tree but with full path
- Print Directories like tree with controlled depth
- Print files in Directories like tree based on matching criteria
- Print files in Directories like tree but with permission details
- Print files in Directories like tree but with owner details
- Print files in Directories like tree but with size details
- Print files in Directories like tree but with last modification time
- Print files in Directories like tree but as an XML
- Summary:
Introduction
When we are using command line mode for accessing a linux operating system, it would be difficult to view all directories and it’s sub directories. Even though linux operating system provides some commands out-of-the box to view the directory structure, it may not be sufficient if we want to display the directory structure in tree view. So we need to install few other libraries to view the directory structure in a tree view. Let’s look at some of the commands that we can use in Linux to view the directory structure in tree view.
Using tree command:
Even though tree command will not be included by default in most of the linux distributions, we can easily install that package using appropriate package manager.
To install tree in Ubuntu or other debian based linux distributions we can use the below command,
sudo apt-get install tree
In redhat, fedora or other RHEL based linux distributions we can use the below command
sudo yum install tree
Print all files and directories like tree
After installing the tree package we can execute the below command to view the directory structure in a tree format
Frequently Asked:
- When to wrap quotes around a shell variable in Linux?
- Find Files by Name in Linux
- Recursively Count Files in a directory in Linux
- Print a directory structure like a tree in Linux
Syntax:
tree
Output :
. ├── tutorial2.txt ├── tutorial.sh └── www └── web ├── css │  └── main.css ├── html │  └── index.html └── script
Print Directories only
Sometimes we want to view only directories by excluding the files in a tree structure, then use the below command
Syntax:
tree -d
Output :
. └── www └── web ├── css ├── html └── script
Print Directories only like tree but with full path
If we want to display the full directory path while displaying the directory path in a tree structure then use the below command
Syntax:
tree -df
Output :
. └── ./www └── ./www/web ├── ./www/web/css ├── ./www/web/html └── ./www/web/script
Print Directories like tree with controlled depth
We can also specify the maximum display depth of the directory tree using the -L option. For example, if you want a depth of 2, then use the below command
Syntax:
tree -f -L 2
Output :
. ├── ./tutorial2.txt ├── ./tutorial.sh └── ./www └── ./www/web
Print files in Directories like tree based on matching criteria
If we want to display only files that matches a specified pattern then we can use -P option as shown below
Syntax:
tree -f -P ind*
Output :
. └── ./web ├── ./web/css ├── ./web/html │  └── ./web/html/index.html └── ./web/script
Print files in Directories like tree but with permission details
In some cases we want to display the permission of each file or directory when we are displaying that in tree view. We can use below syntax to display file/directory permissions
Syntax:
tree -f -p
Output :
. ├── [-rw-r--r--] ./tutorial2.txt ├── [-rw-r--r--] ./tutorial.sh └── [drwxrwxr-x] ./www └── [drwxrwxr-x] ./www/web ├── [drwxrwxr-x] ./www/web/css │  └── [-rw-rw-r--] ./www/web/css/main.css ├── [drwxrwxr-x] ./www/web/html │  └── [-rw-rw-r--] ./www/web/html/index.html └── [drwxrwxr-x] ./www/web/script
Print files in Directories like tree but with owner details
If we want to print the owner of the file in the tree view, then we can use the below syntax
Syntax:
tree -f -pu
Output :
. ├── [-rw-r--r-- opc ] ./tutorial2.txt ├── [-rw-r--r-- opc ] ./tutorial.sh └── [drwxrwxr-x opc ] ./www └── [drwxrwxr-x opc ] ./www/web ├── [drwxrwxr-x opc ] ./www/web/css │  └── [-rw-rw-r-- opc ] ./www/web/css/main.css ├── [drwxrwxr-x opc ] ./www/web/html │  └── [-rw-rw-r-- opc ] ./www/web/html/index.html └── [drwxrwxr-x opc ] ./www/web/script
Print files in Directories like tree but with size details
We can also print the size of each file in tree view using below syntax. We will use -h option to display the file size in human readable format.
Syntax:
tree -f -h
Output :
. ├── [ 0] ./tutorial2.txt ├── [ 0] ./tutorial.sh └── [ 17] ./www └── [ 43] ./www/web ├── [ 22] ./www/web/css │  └── [ 9] ./www/web/css/main.css ├── [ 24] ./www/web/html │  └── [ 0] ./www/web/html/index.html └── [ 6] ./www/web/script
Print files in Directories like tree but with last modification time
If we want to display the last modification time for the file/sub directory then we can use the following syntax.
Syntax:
tree -f -h -D
Output :
. ├── [ 0 Aug 27 4:13] ./tutorial2.txt ├── [ 0 Aug 7 20:14] ./tutorial.sh └── [ 17 Aug 21 18:24] ./www └── [ 43 Aug 21 18:25] ./www/web ├── [ 22 Aug 28 3:23] ./www/web/css │  └── [ 9 Aug 28 3:23] ./www/web/css/main.css ├── [ 24 Aug 21 18:25] ./www/web/html │  └── [ 0 Aug 21 18:25] ./www/web/html/index.html └── [ 6 Aug 21 18:25] ./www/web/script
Print files in Directories like tree but as an XML
If we want to display the tree command as an XML then we can use the following syntax.
Syntax:
tree -f -h -X
Output :
<?xml version="1.0" encoding="UTF-8"?> <tree> <directory name="."> <file name="./tutorial2.txt" size="0"></file> <file name="./tutorial.sh" size="0"></file> <directory name="./www" size="17"> <directory name="./www/web" size="43"> <directory name="./www/web/css" size="22"> <file name="./www/web/css/main.css" size="9"></file> </directory> <directory name="./www/web/html" size="24"> <file name="./www/web/html/index.html" size="0"></file> </directory> <directory name="./www/web/script" size="6"> </directory> </directory> </directory> </directory> <report> <directories>5</directories> <files>4</files> </report> </tree>
Summary:
In this guide, we have learnt multiple ways in which we can display the directory structure in tree format. Thanks.