How to limit output depth?

QuestionsQuestions4 SkillsProPermissions of FilesOct, 29 2025
0134

To limit the output depth when checking disk usage with the du command in a Linux environment, you can use the --max-depth option. This option allows you to specify how many levels of subdirectories you want to display in the output.

Syntax

The basic syntax for using --max-depth is:

du -h --max-depth=N /path/to/directory

Where N is the depth level you want to limit the output to.

Example Usage

  1. Check Immediate Subdirectories: If you want to see the sizes of only the immediate subdirectories within a directory, set N to 1:

    du -h --max-depth=1 ~/project
    

    This command will display the sizes of the project directory and its immediate subdirectories, but not any deeper levels.

  2. Check Two Levels Deep: If you want to see sizes for two levels of subdirectories, set N to 2:

    du -h --max-depth=2 ~/project
    

    This will show the sizes of the project directory, its immediate subdirectories, and their immediate subdirectories.

Example Output

For a directory structure like this:

project/
├── subdir1/
│   ├── file1.txt
│   └── file2.txt
├── subdir2/
│   └── subsubdir1/
│       └── file3.txt
└── file4.txt

Running du -h --max-depth=1 ~/project might produce output like:

4.0K    /home/labex/project/subdir1
8.0K    /home/labex/project/subdir2
12K     /home/labex/project

And running du -h --max-depth=2 ~/project would show sizes for subsubdir1 as well:

4.0K    /home/labex/project/subdir1
4.0K    /home/labex/project/subdir2/subsubdir1
8.0K    /home/labex/project/subdir2
12K     /home/labex/project

Summary

Using the --max-depth option with the du command is a powerful way to control the granularity of the output, making it easier to analyze disk usage without overwhelming detail.

Feel free to ask if you have any further questions or need more examples!

0 Comments

no data
Be the first to share your comment!