du
The du (disk usage) command estimates file space usage. While df tells you how much space is left on a whole partition, du is used to find out which specific directories and files are consuming that space.
Basic Syntax
du [OPTIONS] [FILE or DIRECTORY]
Common Flags/Options
| Option | Description |
|---|---|
-h, --human-readable | Print sizes in human-readable format (e.g., 1K, 234M, 2G). |
-s, --summarize | Display only a grand total for each specified argument (don't list subdirectories). |
-c, --total | Produce a grand total at the very end. |
-a, --all | Write counts for all files, not just directories. |
--max-depth=N | Print the total for a directory only if it is N or fewer levels below the command line argument. |
Real-world Examples
1. Check the total size of a directory
If you want to know exactly how much space the /var/log directory takes up, without seeing every single file inside it:
du -sh /var/log
Output Example:
1.2G /var/log
2. Find the largest directories
A very common task is finding what is eating up disk space. You can combine du with sort.
Note: We use -h for du and -h for sort so the sorting understands Megabytes vs Gigabytes.
sudo du -sh /* | sort -h
3. Analyze usage up to a specific depth
If you are in the root directory / and want to see how much space each top-level folder uses, but you don't want a massive list of all subfolders:
sudo du -h --max-depth=1 /
(In some Unix systems or BSD, the equivalent flag is -d 1).
4. Get a grand total of multiple items
If you want to know the combined size of two different directories:
du -shc /var/log /var/cache
Output Example:
1.2G /var/log
500M /var/cache
1.7G total