The du
(disk usage) command is a powerful utility in Unix-like operating systems used to estimate file space usage. It is essential for understanding how much disk space specific files or directories consume, helping users manage storage effectively.
Understanding the du
Command
The du
command fundamentally displays the number of blocks used for files. When you specify a file as a parameter, it reports its individual disk usage. If the parameter provided is a directory, the command automatically reports on all files within that directory. Should no file or directory parameter be provided, the du
command defaults to using the files in the current directory, showing their respective disk usage.
Basic du
Command Usage for Files and Directories
The most straightforward way to use du
is by simply specifying the target file or directory.
-
For a single file:
du my_document.txt
This command will show the disk usage for
my_document.txt
in kilobytes (by default on most systems, though traditionally in 512-byte blocks). -
For a single directory:
du my_project_folder/
When used on a directory,
du
recursively lists the disk usage for each subdirectory and file withinmy_project_folder/
, culminating in a total for the main directory itself.
Enhancing Readability with du
Options
While the basic du
command provides raw block counts, various options can make the output much more comprehensible and tailored to specific needs.
Common du
Options for Disk Usage Analysis
Option | Description | Example Command |
---|---|---|
-h |
Human-readable output. Displays sizes in K, M, G, etc. | du -h my_document.txt |
-s |
Summarize total disk usage for each argument. Does not list subdirectories. | du -sh my_project_folder/ |
-a |
All files. Displays disk usage for all files, not just subdirectories. | du -ah my_project_folder/ |
-c |
Grand total. Produces a grand total at the end of the output. | du -hc my_folder1 my_folder2 |
--max-depth=N |
Reports total for a directory (or file) only if it is N or fewer levels below the command line argument. | du -h --max-depth=1 my_project_folder/ |
--exclude=PATTERN |
Excludes files that match PATTERN. | du -sh --exclude="*.log" my_data/ |
Practical Examples:
To get a human-readable summary of a directory's total size:
du -sh my_website_assets/
This is particularly useful for quickly checking the size of large project folders.
To see the size of all individual files and subdirectories within a folder, in a human-readable format:
du -ah reports/
This provides a detailed breakdown, showing how space is distributed among individual items.
To find the sizes of all items in the current directory, but only summarize the top-level directories and files:
du -sh *
This gives a quick overview of the largest items without delving into sub-subdirectories.
Advanced du
Usage and Tips
-
Sorting Output: Often, it's useful to sort the
du
output by size to identify the largest files or directories. This can be achieved by piping thedu
output to thesort
command.du -ah . | sort -rh
The
-h
withsort
ensures human-readable sizes are sorted correctly, and-r
reverses the order to show largest first. -
Finding Large Files: To locate the top 10 largest files/directories within a specific path:
du -a /var/log | sort -rh | head -n 10
This command will list the 10 largest log files or directories under
/var/log
. -
Excluding Specific File Types: When calculating disk usage, you might want to exclude certain temporary or backup files.
du -sh --exclude="*.tmp" --exclude="*.bak" my_data_folder/
This command provides the total size of
my_data_folder/
excluding files with.tmp
and.bak
extensions.
The du
command is an indispensable tool for system administrators and everyday users alike, providing clear insights into disk space consumption. For more detailed information, consult the official man du
page on your system or reputable online resources like GNU Coreutils du documentation.