du Command in Linux



The Linux du command displays the estimated disk usage of a file or directory. It is a powerful command line utility that monitors disk usage in Linux.

The du command is one of the standard utilities that can be combined with other utilities for customized output. It can also easily be incorporated with shell scripting.

Table of Contents

Here is a comprehensive guide to the options available with the du command −

Syntax of du Command

The syntax of the Linux du command is as follows −

du [options] [file / directory]

To get the desired output various options can be specified in the [options] field. The disk usage of the file and directory path can be mentioned in the [file / directory] field.

du Command Options

The options used with the du command are listed below −

Flags Options Description
-0 --null It ends the output with NULL, not newline
-a --all It prints the disk usage of all files and directories including the hidden ones
--apparent-size It displays the apparent size (It might differ from the disk usage due to sparse files)
-B size --block-size= size It scales the output by the mentioned size (size can be K, M, or G)
-b --bytes It is equivalent to --block-size=1 or --apparent-size
-c --total It displays the total of the specified file, directory, and files within it
-D --difference-args It displays the disk usage of the target file or directory of a symbolic link
-d N --max-depth= N It displays the disk usage of a directory till the specified depth of the directory
--file0-from= F It displays the disk usage of null-terminated file
-H It is equivalent to -D and --difference-args options
-h --human-readable It displays the disk usage in human-readable form (25K, 100M, 2G)
--inode It displays the disk usage of inodes instead of blocks
-k It is equivalent to --block-size=1K
-L --difference It displays the disk usage of the targe files and directories of symbolic links
-l --count-links It displays the size of both the original file and directory and the hard link
-m It is equivalent to --block-size=1M
-P --no-difference It does not follow the symbolic link (default)
-S --separate-dirs It skips the disk usage of the subdirectories
--si It is equivalent to -h with a base of 1000 instead of 1024
-s --summarize It displays the total size of each specified argument
-t size --threshold= size It excludes the entries by the specified size
--time It displays the last modification time of the specified file, directories, and subdirectories
--time= word It displays the time by the specified word such as atime or ctime
--time-style= style It displays the time by the specified format such as full-iso, long-iso, or +FORMAT
-X file --exclude-from= file It excludes the specified file
--exclude= pattern It excludes files of the specified pattern
-x --one-file-system It excludes directories on a different file system
--help It displays the help of the du command
--version It displays the version of the command

Examples of du Command in Linux

This section demonstrates the usage of the du command in Linux with examples −

  • Displaying the Disk Usage of a File
  • Displaying the Disk Usage of a Directory
  • Displaying Disk Usage in Human-Readable Form
  • Displaying Disk Usage in the Specified Block Size
  • Displaying Disk Usage of All Files and Documents
  • Getting Disk Usage with Null
  • Displaying the Disk Usage Total of All the Output Entries
  • Displaying Disk Usage with Last Modification Time
  • Displaying Disk Usage Excluding Specific Files

Displaying the Disk Usage of a File

To display the disk usage of a file, use the du command and specify the file name −

du file.txt
Displaying Disk Usage of File

To display the disk usage of a file through a path, use −

du Documents/Files/file.txt

Displaying the Disk Usage of a Directory

To display the disk usage of a directory, use the directory name or path −

du directory
Displaying Disk Usage of Directory

Displaying Disk Usage in Human-Readable Form

To display the disk usage in human-readable form, use the -h or --human-readable options −

du -h Documents/Files/file.txt
Displaying Disk Usage in Human-Readable Form

It shows the disk usage with units such as K, M, or G.

Displaying Disk Usage in the Specified Block Size

To display the disk usage in a specific block size, use the -B or --block-size option −

du -B M directory

Displaying Disk Usage of All Files and Documents

To display disk usage of all the files and directories in the specified path, use the -a or --all option −

du -ah directory
Displaying Disk Usage of All Files and Documents

The -h flag displays the disk usage in human-readable form.

Getting Disk Usage with Null

To get du command output with Null instead of newline, use the -0 or --null option. Some applications require null to process the data.

du -0 file.txt

Displaying the Disk Usage Total of All the Output Entries

To display the total disk usage of all the output entries, use the -c option or --total.

du -c directory/
Displaying Disk Usage Total of All Output Entries

It displays a separate entry at the end of the output.

Displaying Disk Usage with Last Modification Time

To display the last modification time of the specified file with the du command, use the --time option −

du --time file.txt

To get the last access and creation time, use the atime or ctime with the --time option. For example, to display the last access time of a file, use −

du --time=atime file.txt

To change the time format, use the --time-style option −

du --time=atime --time-style=+%m-%d-%Y file.txt
Displaying Disk Usage with Last Modification Time

Displaying Disk Usage Excluding Specific Files

To print the disk usage with the du command to exclude a file with a pattern, use the --exclude option with the pattern. For example, to exclude the HTML file, use −

du -a --exclude="*.html" directory
Displaying Disk Usage Excluding Specific Files 1

To exclude files of a specific size, the -t or --threshold option is used. For example, to exclude files of 50KB or less, use

du -t 170K directory
Displaying Disk Usage Excluding Specific Files 2

Displaying Disk Usage by Directory Depth

To display the disk usage by the directory level, -d or --max-depth options are used. For example, to display the disk usage of the parent directory and its first level of subdirectories, the maximum depth will be 1.

du -d 1 directory

Now, lets display the disk usage of the parent directory, its first level of subdirectories, and its subdirectories.

du -d 2 directory
Displaying Disk Usage by Directory Depth

Conclusion

The du command in Linux displays the estimated disk usage of a file or directory. The output can be customized using various options such as filter by file, wild card, or size. Moreover, controlling directory depth, and easy integration with commands make this command a handy tool for system administrators.

In this tutorial, we explained the du command, its syntax, options, and usage through various examples.

Advertisements