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
Check Immediate Subdirectories: If you want to see the sizes of only the immediate subdirectories within a directory, set
Nto 1:du -h --max-depth=1 ~/projectThis command will display the sizes of the
projectdirectory and its immediate subdirectories, but not any deeper levels.Check Two Levels Deep: If you want to see sizes for two levels of subdirectories, set
Nto 2:du -h --max-depth=2 ~/projectThis will show the sizes of the
projectdirectory, 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!
