> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# tree Command Examples in Linux
- URL: https://linuxhandbook.com/tree-command/
- Published: 2022-12-12T10:44:17.000Z
- Updated: 2022-12-15T11:37:24.000Z
- Description: The tree command is excellent for viewing the structure of a directory and its subdirectories in Linux. Learn how to use it with practical examples.
- Author: Sagar Sharma
- Tags: Commands

You are already familiar with the [directory structure in Linux](https://linuxhandbook.com/linux-directory-structure/). It is like the roots of a tree.

And that's exactly the concept of the tree command. It shows the contents of the current directory and its subdirectories in a tree like fashion.

![Tree command in Linux](https://linuxhandbook.com/content/images/2022/12/tree-command-examples.png)

Before showing the examples of the tree command, let me quickly discuss how to install it.

## Install tree

The tree utility does not come pre-installed in most Linux distributions. But you can find it in the official repositories and easily install it.

For Debian/Ubuntu base:

```
sudo apt install tree
```

For RHEL base:

```
sudo yum install tree
```

For Arch-based distros:

```
sudo pacman -S tree
```

Once done, all you need to do is append the directory or directory path to the `tree` command and it will show file contents in a tree manner:

```
tree target_directory
```

![](https://linuxhandbook.com/content/images/2022/12/use-tree-command-to-list-files-in-tree-manner-in-linux-1.png)

But the tree command can do more than just listing files in Linux. Here are some handful examples. 

## List only directories

If you want to [list only the directories](https://linuxhandbook.com/list-only-directories/) at the specified location, you can use the `-d` option.

```
tree -d target_directory
```

![list directories only using the tree command](https://linuxhandbook.com/content/images/2022/12/list-directories-only-using-the-tree-command.png)

## List hidden files

By default, the tree command won't [list hidden files](https://linuxhandbook.com/ls-show-hidden-files/) but this behavior can be altered by using the `-a` option.

```
tree -a target_directory
```

And here, I have compared how it differs when used alongside the default manner (without any options):

![find hidden files in linux using tree command](https://linuxhandbook.com/content/images/2022/12/find-hidden-files-in-linux-using-tree-command-1.png)

## Include the path of files

If you want to [get the path for each file](https://linuxhandbook.com/get-file-path/), all you need to do is use the `-f` option and it gets you the path of every file:

```
tree -f directory
```

![list path of every file in directory using the tree command](https://linuxhandbook.com/content/images/2022/12/list-path-of-every-file-in-directory-using-the-tree-command.png)

**But what about getting the full path?** Well, you just have to append the full path of a directory (from home to the target one) as shown:

```
tree -f /home/sagar/Directory
```

And it is lengthy. So let me share a neat way.

In this case, you can utilize the value `pwd` that holds the value full path. So change the directory where you want to use the tree command.

And use the following command and it will get you the full path of every file present in that directory:

```
tree -f "$(pwd)"
```

![include full path of every file in tree command](https://linuxhandbook.com/content/images/2022/12/include-full-path-of-every-file-in-tree-command.png)

## List files and directories based on the level

So if the directory has hundreds of subdirectories and if you only want to list the first certain levels such as want to include the first one or two subdirectories, you can use this feature.

You can specify the levels by appending the level (in numbers) to the `-L` option:

```
tree -L Level
```

For example, here, I listed files that are at level 2:

```
tree -L 2
```

![list files based of file levels in tree command](https://linuxhandbook.com/content/images/2022/12/list-files-based-of-file-levels-in-tree-command.png)

And you can see the difference where **on the left side, it showed every file present** whereas, on the **right side (where I used level 2), it only brought the files & directories placed at level 2**.

## List files with permission

To attach [file permissions](https://linuxhandbook.com/linux-file-permissions/) with every file, all you need to do is append the `-p` option:

```
tree -p TargetDirectory
```

But to make it convenient, I would suggest pairing it with `-h` option for better readability. 

For example, Here I went with listing every file present in the `MUSIC` directory:

```
tree -ph MUSIC
```

![List files with permission using the tree command](https://linuxhandbook.com/content/images/2022/12/List-files-with-permission-using-the-tree-command.png)

## Get the file size of a directory using the tree command

The tree command can [show you the size of each file and directory](https://linuxhandbook.com/find-directory-size-du-command/) at a specified location and will also sum the size for you in the end. 

For that, you will have to use `--df` option and I would recommend pairing it with the `-h` for better readability: 

```
tree --df -h TargetDirectory
```

![Get the file size of a directory using the tree command](https://linuxhandbook.com/content/images/2022/12/Get-the-file-size-of-a-directory-using-the-tree-command.png)

## List files based on the last modification using the tree command

There are two ways of sorting files based on modification:

- First modified
- Last modified

### Sort files based on first modification 

To sort files based on modification, you will have to use the `-c` option and it will sort files that were modified the first by default.

And here, I will also be using the `-D` option to get the date and time of modification:

```
tree -cD TargetDirectory
```

![List the first modified files using the tree command](https://linuxhandbook.com/content/images/2022/12/List-the-first-modified-files-using-the-tree-command.png)

### Sort files based on the last modification

In this case, you will have to alter the default effect of the `-c` option which will list files based on the first modification.

Which can easily be altered when paired with the `-r` option which will reverse the effect of `-c` option:

```
tree -cDr TargetDirectory
```

![sort files based on last modification using the tree command in linux](https://linuxhandbook.com/content/images/2022/12/sort-files-based-on-last-modification-using-the-tree-command-in-linux.png)

And you have them sorted for you!

## Wrapping up

You can see how easy it is to visualize a nested directory structure with the tree command.

Ever since I installed it first, it's been an essential part of my Linux command arsenal. I hope you also start using it extensively.