Limiting Directory Tree Depth
When working with large directory structures, viewing the entire tree can be overwhelming. In this step, you will learn how to limit the depth of the displayed directory tree.
Creating a Deeper Directory Structure
Let's create a more complex directory structure to better demonstrate this feature:
mkdir -p ~/project/backups/2023/{January,February,March}/{week1,week2,week3}
touch ~/project/backups/2023/January/week1/data.bak
This creates:
- A
backups
directory with a structure similar to our logs
- An additional level for weeks within each month
- A sample backup file in January's week1 folder
Using the -L Option to Limit Depth
The -L
option allows you to specify how many levels deep the tree should display. Let's limit our view to 3 levels:
tree -L 3
You should see an output similar to this:
.
├── backups
│ └── 2023
│ ├── January
│ ├── February
│ └── March
└── logs
└── 2023
├── January
├── February
└── March
Notice that the week directories and files within them are not shown because they would be at level 4, and we limited the display to 3 levels.
Changing the Depth Level
You can adjust the number after -L
to show more or fewer levels. Let's try with a depth of 4:
tree -L 4
Now you should see the week directories but not the files inside them:
.
├── backups
│ └── 2023
│ ├── January
│ │ ├── week1
│ │ ├── week2
│ │ └── week3
│ ├── February
│ │ ├── week1
│ │ ├── week2
│ │ └── week3
│ └── March
│ ├── week1
│ ├── week2
│ └── week3
└── logs
└── 2023
├── January
│ └── log1.txt
├── February
│ └── log2.txt
└── March
└── log3.txt
Being able to control the depth of the tree is useful when you only need a high-level overview of a complex directory structure.