tree in Linux
Tags: linux, tutorial, productivity
Published: 2021-06-14 15:23:22

## `tree` in Linux

`tree` command helps to recursively list the directory and its details in a tree format. In this post, we will see how you can use `tree` effectively to view the directory details.

## `tree` essentials in Linux

To install `tree` in Ubuntu, Debian Linux distros.

```
sudo apt-get install tree
```

To install `tree` in CentOS:

```
sudo yum install tree
```

In Windows, you can issue `tree` in Windows Terminal.

## Enter `tree`

If you enter `tree` command without any arguments, it will list out everything from the current directory recursively. Suppose, if you are in your home directory, it will display everything recursively like below.

![Tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ccwzh8gpqo72rky5hba.gif)

## Man page

```
man tree
```

## List all contents including hidden files and file starts with `.`
```
tree -a
```

## List all directories recursively
```
tree -d
```

## Print file patch prefix
```
tree -f
```

## Print details by max display depth

```
tree -L 1
```
![Display Depth 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ae53gwl3w268j0gvvg3.jpg)

## Print details by pattern
```
tree -P b*
```
![Print details by pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9yxb7rzq1edqkrk88e2j.jpg)

```
tree backups -P 'SMTP*'
```
![Print details by pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12gw2xsmid8p91dflwhw.jpg)
` '*' - any zero or more characters `
` '?' - any single character `
` '[...]' - any single character listed between brackets `
` '[A-Z]' - for range `
` '[^...]'- any single character not listed in brackets `
`  '|' - separates alternate patterns `

## Print details not matching the pattern
```
tree backups -I 'SMTP*'
```
![Print details not matching the pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6yugtpte42rt4zhap7o.png)
 
## --matchdirs and --prune

If you use `-P` which matches both the directories and files. If the match is found for directories, then the matching is disabled for its contents. Use it with --prune to prune it.

![--matchdirs and --prune](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zp8hgqqssjsilnwzwof.png)

## --noreport

To not to print the file and directory report, use `--noreport`.

```
tree bin -P '*.log' --prune --noreport 
```

## JSON Output

```
tree bin -P '*.log' --prune -J
```

## XML Output
```
tree bin -P '*.log' --prune -X
```

## Print last modification date/time
```
tree bin -P '*.log' --prune -D
```

## Print size

```
tree bin/ -P '*.properties' --prune -h
```
![Print size](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlm6ftyche0elo670bh2.png)

## Mark directories and files

`-F` marks directories as `*` and `/` for files.

```
tree -P '*.jmx' --prune -F
```

![Mark directories and files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co3a5uavn378m1d1bxu0.png)

### --dirsfirst

It displays directories first over files. To disable, use `-U`.

```
tree -P '*.jtl' --prune --dirsfirst
```

![--dirsfirst](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9osasrgqdbv3tqrk8iyu.png)

![--dirsfirst](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3mgztyagmjrqkk3qvi8.png)
 
In this post, we have covered frequently used tree commands. Please check my [blog](https://qainsights.com) for more such tutorials.

💙 [QAInsights YouTube Channel](https://youtube.com/qainsights)