LinuxCommandLibrary

du

Show disk space usage

TLDR

List the sizes of a directory and any subdirectories, in the given unit (B/KiB/MiB)

$ du -[b|k|m] [path/to/directory]
copy

List the sizes of a directory and any subdirectories, in human-readable form (i.e. auto-selecting the appropriate unit for each size)
$ du [[-h|--human-readable]] [path/to/directory]
copy

Show the size of a single directory, in human-readable units
$ du [[-sh|--summarize --human-readable]] [path/to/directory]
copy

List the human-readable sizes of a directory and of all the files and directories within it
$ du [[-ah|--all --human-readable]] [path/to/directory]
copy

List the human-readable sizes of a directory and any subdirectories, up to N levels deep
$ du [[-h|--human-readable]] [[-d|--max-depth]] N [path/to/directory]
copy

List the human-readable size of all .jpg files in current directory, and show a cumulative total at the end
$ du [[-ch|--total --human-readable]] [./*.jpg]
copy

List all files and directories (including hidden ones) above a certain threshold size (useful for investigating what is actually taking up the space)
$ du [[-ah|--all --human-readable]] [[-t|--threshold]] [1G|1024M|1048576K] .[^.]* *
copy

SYNOPSIS

du [OPTION]... [FILE]...

PARAMETERS

-h, --human-readable
    Display sizes in human-readable format (e.g., 1K, 234M, 2G).

-s, --summarize
    Display only a total for each argument, rather than individual entries.

-c, --total
    Produce a grand total of all arguments processed.

-a, --all
    Write counts for all files, not just directories.

-x, --one-file-system
    Skip directories on different file systems.

--max-depth=N
    Print the total for a directory (or file) only if it is N or fewer levels below the command line argument. `--max-depth=0` is equivalent to `-s`.

DESCRIPTION

The du command, which stands for disk usage, is a standard Unix/Linux utility used to estimate file space usage. It summarizes the disk usage of the set of files or directories specified, recursively traversing subdirectories by default. This makes it an invaluable tool for identifying which files or directories are consuming significant amounts of disk space on a system. By default, du reports sizes in 1KB blocks, but it offers options to display sizes in more human-readable units like kilobytes, megabytes, or gigabytes.

Unlike df, which reports free disk space on file systems, du focuses on the actual space consumed by files and their contents, including allocated blocks for directories themselves. It provides a granular view of storage consumption, helping system administrators and users manage disk space efficiently. The command calculates usage by inspecting the directory entries and inode information for files and directories within the specified path.

CAVEATS

du reports the space allocated on disk, which might be larger than the actual logical size of a file (e.g., for sparse files).
By default, du counts disk space in 1KB blocks, which can be confusing without the -h (human-readable) option.
It can be slow on very large directory trees due to extensive file system traversal.
Finally, it only shows space used by files and directories accessible to the user running the command, respecting file system permissions.

DU VS. DF

While du reports the disk space used by specific files or directories, df reports the free disk space on mounted file systems. du traverses the file system to sum up sizes, while df reads file system metadata directly.

COMMON USAGE EXAMPLES

Common Usage Examples:
To get the total size of the current directory in human-readable format:
du -sh .

To list human-readable sizes of all files and first-level subdirectories within /var/log:
du -ah --max-depth=1 /var/log

To list human-readable sizes of all items in the current directory, sorted by size (largest first):
du -sh * | sort -rh

HISTORY

The du command is a fundamental utility that has been a part of Unix-like operating systems since their early days, originating from the AT&T Unix System III development. It has been a staple in almost every Unix distribution and its derivatives, including Linux. The version commonly found on Linux systems is part of the GNU Core Utilities, which provide robust and feature-rich implementations of standard Unix commands. Its core functionality of reporting disk usage has remained consistent, with enhancements over time focusing on additional options for output formatting and control over recursion depth.

SEE ALSO

df(1), ls(1), find(1), stat(1)

Copied to clipboard