What it is
du estimates file space usage, typically used to find out how much disk space specific files or directories are consuming.
Installation
Linux:
du is a core utility and is pre-installed on most Linux distributions. If missing, install via your package manager:
sudo apt update && sudo apt install coreutils # Debian/Ubuntu
sudo yum install coreutils # CentOS/RHEL
sudo dnf install coreutils # Fedora
Mac:
du is a core utility and is pre-installed on macOS.
Windows:
du is not a native Windows command. You can install it via:
- Git Bash: If you have Git for Windows installed,
duis available within the Git Bash terminal. - WSL (Windows Subsystem for Linux): Install a Linux distribution from the Microsoft Store and use
duwithin that environment.
Commands / Usage
Basic Usage
-
Check current directory’s total size:
duShows disk usage for all subdirectories in the current directory, in blocks (usually 1KB).
-
Check a specific directory’s total size:
du /var/logShows disk usage for
/var/logand its subdirectories. -
Check size in human-readable format:
du -hShows disk usage with sizes in KB, MB, GB (e.g.,
1.5G,250M). -
Check a specific directory’s size in human-readable format:
du -h /home/user/documentsShows disk usage for
/home/user/documentsand its subdirectories in human-readable format. -
Display only the total size of a directory (summary):
du -sh /home/user/projectsShows the grand total size of
/home/user/projectsin human-readable format.
Controlling Depth and Output
-
Show usage for the current directory and its immediate subdirectories only:
du -h --max-depth=1Shows disk usage for the current directory and only one level of subdirectories.
-
Show usage for a specific directory and its immediate subdirectories only:
du -h --max-depth=1 /etcShows disk usage for
/etcand its immediate subdirectories. -
Show usage for all files and directories, including hidden ones:
du -aLists disk usage for all files and directories, not just directories.
-
Sort output by size:
du -h | sort -rhLists disk usage for all subdirectories, sorted from largest to smallest.
-
Find the largest directories (top 5):
du -h --max-depth=1 | sort -rh | head -n 5Finds the 5 largest directories (including the current directory) in the current location.
Appending Information
-
Append human-readable size to each line:
du -h -aSame as
du -a, but with human-readable sizes. -
Append apparent size (actual file size) instead of disk usage:
du -h --apparent-sizeShows the actual size of files, which can be smaller than disk usage due to sparse files or block allocation.
-
Append apparent size and sort by it:
du -h --apparent-size -a | sort -rhLists files and directories sorted by their actual size.
Specifying Block Size
-
Specify block size (e.g., 1MB blocks):
du -BMShows disk usage in megabyte blocks.
du -BGfor gigabytes. -
Combine with human-readable (shows block size in KB, MB, GB):
du -h -kShows sizes in kilobytes (default for
-k).-mfor megabytes,-gfor gigabytes.
Excluding Directories
-
Exclude a specific directory from the scan:
du -h --exclude='node_modules'Calculates disk usage for the current directory, skipping anything named
node_modules. -
Exclude multiple directories:
du -h --exclude='node_modules' --exclude='*.log'Excludes directories named
node_modulesand any files ending in.log.
Common Patterns
-
Find the top 10 largest directories in the home folder:
du -h --max-depth=1 /home/user | sort -rh | head -n 10 -
Find the largest files (not just directories) in the current directory:
du -a . | sort -rh | head -n 10Note:
du -aincludes directories themselves. -
Find the largest files (actual size) in the current directory:
du -ah --apparent-size . | sort -rh | head -n 10 -
Check disk usage of web server logs:
sudo du -sh /var/log/apache2 -
Check disk usage of Docker images (requires root/sudo):
sudo du -sh /var/lib/docker/images/*(This path might vary based on Docker installation)
-
Find directories larger than a certain size (e.g., 1GB):
du -h --max-depth=1 | awk '$1 ~ /[0-9.]+G/ && $1+0 > 1 {print $0}'This is a bit more advanced, using
awkto filter lines where the size (first column) contains 'G' and is numerically greater than 1.
Gotchas
- Permissions:
dumight report "permission denied" errors for directories you don’t have read access to. Usesudo dufor a more complete picture if needed. - Block Size vs. Apparent Size: By default,
dureports the disk space allocated for files, which can be larger than the actual file size due to filesystem block allocation. Use--apparent-sizeto see the actual file size. This is crucial for understanding the true size of sparse files. - Hidden Files/Directories:
duby default only reports on directories. To see files as well, use the-aflag. Hidden files (starting with.) are included by default if-ais used. - Live Filesystems:
ducalculates usage based on the filesystem’s metadata. For very active filesystems with many writes, the reported size might not be perfectly instantaneous. - Mount Points:
duby default will traverse into mounted filesystems. Use the-x(or--one-file-system) flag to restrict the scan to the filesystem of the starting directory.
This will calculate usage for the root filesystem only, not descending into other mounted partitions or network shares.du -xh /