Effective disk space management is an indispensable skill for any Linux user or administrator. Neglecting disk space can lead to system instability, application failures, and significant performance degradation. This guide will critically examine the df and du commands, providing a precise, direct approach to understanding and utilizing them for monitoring and managing disk usage. By the end, you will confidently identify available space, locate storage hogs, and maintain a healthy system.
Prerequisites
To follow this guide, you will need:
- Access to a Linux command-line interface (CLI).
- Basic familiarity with executing commands in a terminal.
Understand `df` for Filesystem-Wide Usage
The df (disk free) command reports the amount of disk space used and available on mounted filesystems. It provides a high-level overview, crucial for understanding overall partition health. Critically, df operates on filesystems, not individual directories or files.
Execute a Basic Disk Free Check
To see the default output, which can be somewhat difficult to parse for exact sizes:
df
Pro-Tip: The default output often uses 1K blocks, making it less intuitive for human comprehension. Always refine your output.
Display Human-Readable Output
For a far more practical and readable output, use the -h (human-readable) option. This converts sizes into KB, MB, GB, etc.:
df -h
Warning: While df -h is convenient, scripting might require the precise, non-human-readable output for consistent parsing.
Include All Filesystems
By default, df might omit certain pseudo-filesystems (like tmpfs or devtmpfs). To include all filesystems, use the -a (all) option:
df -ah
Practical Tip: This is useful for debugging issues where even small, ephemeral filesystems might be unexpectedly consuming resources.
Show Filesystem Type
To ascertain the type of each filesystem (e.g., ext4, xfs, nfs), employ the -T (type) option:
df -hT
Use Case: Identifying network-mounted filesystems (NFS, CIFS) or verifying that a partition uses the expected filesystem type.
Check Inode Usage
Sometimes, a disk can appear full even if the reported data usage is low. This often indicates a shortage of inodes (index nodes), which store metadata about files and directories. Use -i (inodes) to check inode usage:
df -hi
Pro-Tip: Running out of inodes can prevent file creation, even if gigabytes of free space remain. This is a common, yet often overlooked, issue on systems with many small files.
Utilize `du` for Directory-Specific Usage
The du (disk usage) command estimates file space usage. Unlike df, du traverses directories and sums up the space consumed by files and subdirectories within a specified path. This is invaluable for pinpointing specific directories or files that are consuming excessive space.
Perform a Basic Directory Usage Scan
To display the disk usage for the current directory and its subdirectories (often verbose):
du
Warning: Running du without options on a large directory tree can produce an overwhelming amount of output and take a significant amount of time.
Summarize Current Directory Usage in Human-Readable Format
To get a concise summary of the disk space used by the current directory, use -sh (summary, human-readable):
du -sh .
Practical Tip: This is your go-to command for a quick check of how much space a project directory occupies.
List Sizes of Contents in Current Directory
To see the size of each file and subdirectory directly within the current directory, use -sh *:
du -sh *
Use Case: Quickly identifying which immediate subdirectories or files are the largest contributors to disk usage in a given path.
Sort and Identify Largest Directories
Combine du with sort to find the largest directories within a path. This is a powerful technique for identifying space hogs:
du -ah | sort -rh | head -n 10
This command lists all files and directories, sorts them by size in reverse human-readable order, and shows the top 10. The -a option includes individual files, while -h makes the sizes readable. -r reverses the sort, and -h for sort ensures human-readable comparison.
Pro-Tip: This combination is arguably the most critical for proactive disk space management, enabling you to swiftly locate and address large files or directories.
Calculate Grand Total for a Directory
If you need the total space used by a directory and all its contents, along with individual item breakdowns, add the -c (total) option:
du -hc /var/log
This will list the size of each item within /var/log and then provide a grand total at the end.
Practical Scenarios for Disk Space Management
Effectively combining df and du allows for a systematic approach to disk space issues.
Identify a Full Filesystem
Start with df -h. If a partition shows 90% or more usage, it’s a critical situation requiring immediate attention.
Locate the Culprit Directory
Once you identify a full partition (e.g., /dev/sda1 mounted on /), navigate to its mount point (cd /) and use du -sh * to see which top-level directories are largest. Recursively descend into the largest directories, repeating du -sh * or du -ah | sort -rh | head -n 10 until you pinpoint the exact files or subdirectories consuming excessive space.
Address Common Space Hogs
- Log Files: Check
/var/log. Old, unrotated logs can grow immensely. - Temporary Files: Examine
/tmpand user-specific temporary directories. - Package Caches: For Debian/Ubuntu,
/var/cache/apt/archives; for RHEL/CentOS,/var/cache/dnfor/var/cache/yum. - User Data: Large media files, backups, or downloads in user home directories (
/home).
These commands are foundational tools for maintaining a robust and efficient Linux system. Consistent monitoring and timely intervention, guided by df and du, prevent unforeseen system failures.
Explore interactive tools like ncdu (NCurses Disk Usage) for a more visual and navigable experience when analyzing disk space, especially on remote servers.
