Mastering Disk Space: A Guide to Checking Disk Usage in Linux with `df` and `du`

Running out of disk space on a Linux system can bring essential services to a grinding halt. Proactively checking disk usage in Linux is a critical skill for any system administrator or user. Thankfully, Linux provides powerful command-line tools to help you monitor and analyze storage. This guide delves into the two most essential commands: `df` (disk free) and `du` (disk usage), explaining how they work, their differences, and how to use them effectively.
Understanding how much space is available and identifying what’s consuming it are the first steps towards efficient disk management. Let’s explore how `df` and `du` help you achieve this.
Understanding `df`: The Filesystem Overview
The `df` command is your go-to tool for getting a high-level overview of disk space usage across all mounted filesystems. It stands for “disk free” or “disk filesystem” and reports statistics directly from the filesystem’s metadata.
Running `df` without any options might produce output that’s hard to read, showing sizes in 1-kilobyte blocks:
Filesystem 1K-blocks Used Available Use% Mounted on udev 4018200 0 4018200 0% /dev tmpfs 808868 1772 807096 1% /run /dev/sda1 51475068 30123456 18711612 62% / ...[Hint: Insert image showing basic `df` command output here]
To make this more understandable, the `-h` (human-readable) option is invaluable:
$ df -h Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 790M 1.8M 789M 1% /run /dev/sda1 49G 29G 18G 62% / ...
Key `df` Options:
-h
: Print sizes in human-readable format (e.g., 1K, 234M, 2G).-T
: Include the filesystem type (e.g., ext4, xfs, tmpfs) in the output. This helps identify different storage types.-i
: Display inode usage instead of block usage. Running out of inodes (data structures storing file metadata) can also prevent file creation, even if disk space is available.-a
: Show all filesystems, including pseudo, duplicate, or inaccessible ones.
Essentially, `df` provides a quick health check for your storage at the partition or filesystem level. It tells you how much total space you have and how much is currently free on each mounted volume.
Diving Deeper with `du`: Finding Space Hogs
While `df` tells you the overall picture, `du` (disk usage) helps you pinpoint *what* is consuming the space within specific directories and files. It works by recursively summing up the sizes of files within a given path.
Running `du` in a large directory can produce a lot of output, listing the size of every subdirectory. Like `df`, using `du` with the `-h` option is highly recommended:
$ du -h /var/log 1.2M /var/log/journal/e1... 1.3M /var/log/journal 4.0K /var/log/apt 8.0K /var/log/unattended-upgrades 24K /var/log/installer ... 15M /var/log[Hint: Insert image showing basic `du -h` command output in a directory]
Useful `du` Options:
-h
: Print sizes in human-readable format.-s
: Display only a total summary for each specified argument (directory), rather than listing subdirectories.-c
: Produce a grand total at the end of the output.--max-depth=N
: Limit the output to directories N levels deep from the starting point. For example,--max-depth=1
shows the size of only the immediate subdirectories.-a
: Show sizes for individual files as well as directories.
du
is crucial when `df` shows a filesystem is nearly full, but you don’t know where the space has gone. You can use `du` to navigate through directories and identify the largest culprits.
Combining `du` with Other Commands for Checking Disk Usage in Linux
The real power of `du` often comes when combined with other commands like `sort` (to order results) and `head` (to show the top offenders). Here’s a common pattern to find the top 10 largest directories within `/var`:
$ sudo du -sh /var/* | sort -rh | head -n 10 1.5G /var/lib 500M /var/cache 15M /var/log ...[Hint: Insert video demonstrating finding large directories using `du`, `sort`, and `head`]
Breaking this down:
sudo du -sh /var/*
: Calculates the summarized (-s
) human-readable (-h
) size of each item (*
) directly within/var
. `sudo` is often needed to access all directories.sort -rh
: Sorts the output numerically (-n
implied by-h
in GNU sort) in reverse (-r
, largest first) order.head -n 10
: Displays only the top 10 lines of the sorted output.
Why `df` and `du` Might Disagree
Sometimes, you might notice a discrepancy between the used space reported by `df` for a filesystem and the total size reported by `du` for the root directory of that filesystem. This happens because:
- Filesystem Overhead: `df` accounts for space used by the filesystem itself (metadata, journals, reserved blocks), which `du` doesn’t see as regular files. Learn more about filesystem structures like inodes at the Linux Foundation.
- Open File Handles: If a process has deleted a large file but still holds it open, `df` will show the space as used (because it’s not yet released by the filesystem), while `du` won’t be able to find or sum the deleted file. The `lsof` command can help identify such processes.
- Mount Points: If another filesystem is mounted onto a directory, `df` shows the usage of the mounted filesystem. `du`, however, will only measure the files *within* that directory on the parent filesystem unless it traverses into the mount point (which might not be what you intended if comparing against `df`).
Understanding these differences is key to accurately interpreting the output when checking disk usage in Linux.
Conclusion: Using the Right Tool for the Job
Effectively checking disk usage in Linux relies on understanding and using both `df` and `du`. Use `df` for a quick, filesystem-level overview of available space. When you need to investigate *what* is consuming space within a specific filesystem or directory, turn to `du`, often combined with `sort` and `head`, to pinpoint the largest files and folders.
Regularly monitoring disk usage with these tools helps prevent storage-related outages and keeps your Linux system running smoothly. For further reading on system administration tasks, check out our guide on managing user permissions in Linux.