Skip to content

The du Command: Disk Usage

Summary

The du command is a powerful tool for estimating file space usage. It recursively summarizes disk usage of each file and directory within a specified path.

Introduction

The du command (short for "disk usage") is a standard Unix command used to estimate the file space usage under a particular directory or file. It is an essential tool for system administrators and developers who need to monitor disk space and identify directories consuming the most storage. Unlike simply looking at file sizes, du takes into account block sizes and other factors, providing a more accurate representation of actual disk usage.

Use case and Examples

Basic Disk Usage

du .
This command shows the disk usage of the current directory and all its subdirectories in kilobytes.

Disk Usage in Human-Readable Format

du -h .
This command displays the disk usage in a human-readable format (e.g., KB, MB, GB). Much easier to understand at a glance!

Summarize Total Disk Usage

du -sh .
This command provides the total disk usage of the current directory (and everything within it) in a human-readable format. The -s flag tells du to only show the sum.

Sorting by Size

du -sh * | sort -h
This command displays the disk usage of all files and directories in the current directory in human readable format, then sorts them by size, making it easy to identify the largest consumers of disk space. Note that sort -h needs GNU sort to work properly.

Disk Usage of Specific Directory

du -sh /var/log
This command shows the total disk usage of the /var/log directory in a human-readable format.

Commonly used flags

Flag Description Example
-a Display an entry for all files and directories. By default, du only shows directories. du -ah . (shows all files and directories, human-readable format)
-h Print sizes in human readable format (e.g., 1K 234M 2G). du -h /home (shows disk usage of /home in human-readable format)
-s Display only a total for each argument. du -s /etc (shows the total disk usage of /etc)
-k Print sizes in kilobytes. This is often the default. du -k . (shows disk usage in kilobytes)
-m Print sizes in megabytes. du -m . (shows disk usage in megabytes)
--exclude='PATTERN' Exclude files that match PATTERN. du -sh --exclude='*.log' . (shows the total disk usage, excluding all files ending with .log)


Share on Share on

Comments