disk usage top level directories

How to Show Disk Usage Only for Top Level Directories

Du is the most commonly used command to calculate and view disk usage in Linux. By default, it calculates the disk space occupied by all files, directories and sub directories located at specific path. But this can be tedious since du command recursively calculates disk space as long as there are subfolders and files present in each directory of your input location. Sometimes you may need to just quickly calculate the disk space occupied by directories at the first level, and not more. Based on the displayed output, you may want to investigate further. In this article, we will learn how to show disk usage only for top level directories.


How to Show Disk Usage Only for Top Level Directories

There are several ways to view disk usage in Linux.

1. Using max-depth option

The max-depth option allows you to specify the depth to which du command must continue calculating disk space. To show disk usage, you can specify max-depth=1 as shown below.

$ du -h --max-depth=1

The above command will display disk usage for all top directories in your present working directory. If you want to view this information for another location, specify it before the options. Here is the command to display disk usage for top level directories at /home/ubuntu.

$ du /home/ubuntu -h --max-depth=1

If you set max-depth=2 it will show disk usage for directories up to 2 levels.

2. Using d flag

Alternatively, you can also use -d flag followed by the depth to which you want du command to calculate disk usage. Here is the command to calculate disk usage for top level directories at present location.

$ du –h -d1

If you want to calculate disk usage for top level directories at another location, you can specify it before the options. Here is the command to get disk usage for all top level directories at /home/ubuntu.

$ du /home/ubuntu –h -d1

If you want to display disk usage for directories upto depth=2, you need to specify 2 after -d option.

$ du –h -d2

By default, du command displays all disk space information in number of bytes. This can be non-intuitive. So in all the above commands, we have used -h option to display the disk space in human readable formats such as Kb, Mb, Gb, etc.

In this article, we have learnt how to show disk usage of top level directories. du is a very useful Linux command to view disk usage information in various formats.

Also read:

How to Resolve bin/sh : 1 source not found
How to Reload etc/hosts in Linux
How to Kill Stopped Jobs in Linux
How to Change MAC Address in Linux
How to Convert Permissions to Octal in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *