du stands for disk usage. As the name suggests, the du command is used to calculate or summarize the disk usage of files, directories & file systems in a UNIX-like operating system.

Syntax

# du [OPTION]... [FILE]...
# du [OPTION]... --files0-from=F

From the syntax, it is clear that the du command expects options and file names as arguments. Though these arguments are not mandatory but are required for getting disk usage of specific files in specific formats. Input file names can also be read from a separate file.

Options:

Option Description
-a display an entry for each file contained in the current directory
-c display a grand total of the disk usage found by the other arguments
-d the depth at which summing should occur. -d 0 sums at the current level, -d 1 sums at the subdirectory, -d 2 at sub-subdirectories, etc.
-h display the result in human readable format
-k show sizes as multiples of 1024 bytes, not 512-byte
-L calculate disk usage for link references anywhere
-s report only the sum of the usage in the current directory, not for each file
-x only traverse files and directories on the device on which the pathname argument is specified.
-X ,-exclude-from=FILE : exclude files that match any pattern in FILE

Example 1: disk usage of a directory, when du command executed without any options.

# du  Pictures
12072 Pictures

In the above example, ‘Pictures’ is a directory and when du command is executed, it displays disk usage figures in Kilobytes (default block size).

Example 2: Display sizes in human readable format (e.g. 1K, 250M, 2G etc.)

# du -h Pictures
 12M   Pictures

Example 3: Change the default block size to produce output in megabytes or gigabytes.
This can be done through -B option followed by ‘M’ for megabyte and ‘G’ for gigabyte.

# du -BM /var/lib/mysql/*

disk usage du command in Linux

The above command shows disk usage in MB.

# du -BG /var/lib/mysql/plist/
5G      /var/lib/mysql/plist/

To display the disk usage in GB, use the G option as shown above.

Example 4: Display the sum usage of all the directories (-s) in human-readable format.

# du -sh /var/log/
299M   /var/log/

The above command shows the sum of disk usage of all the files and directories under /var/log/.

Example 5: Display disk usage counts for all the files. This can be done using -a option.

du command usage in Linux

As shown in the above example, that disk usage of individual files (inside the directory ‘/var/log/httpd’) is displayed in the output.

Example 6: Display the grand total of disk usage for individual files in a directory.
This can be done through -c along with -a option.

du command in Linux

Example 7: Disk usage of all sub-directories and files including hidden files within the current directory (sorted by file size)

# du -sh .[!.]* * | sort -n

Example 8: Disk usage of a sub-directories under root directory (-D 1, trailing /) with a sum total at the end (-c), all displayed in human-readable format (-h) without traversing into other file systems (-x).

# du --max-depth=1 -c -h -x /

du command examples in Linux

Example 9: Estimate disk usage by excluding particular file type.

# du -cbha --exclude="*.iso"

The above command will calculate the disk usage of all the files and directories in the current directory by excluding .iso files.

du command options in Linux