Checking Disk Usage (df, du)
1. Introduction to Disk Usage Monitoring
Disk space is a critical resource in Linux systems. Proper monitoring ensures that systems remain functional and avoid unexpected downtime caused by insufficient storage. It is especially important for:
- Preventing system failures: Running out of disk space can halt important processes.
- Maintaining performance: Low disk space can lead to slower I/O operations.
- Analyzing and optimizing storage usage: Identifying large or unnecessary files helps free up space.
Linux provides two essential commands for monitoring disk usage:
df
(Disk Filesystem): Provides an overview of disk usage for mounted filesystems.du
(Disk Usage): Examines detailed usage for specific files and directories.
2. Using the df
Command – Disk Filesystem Overview
The df
command is primarily used to display the total, used, and available disk space on all mounted filesystems.
Syntax:
df [OPTION] [FILE]
Examples:
1. Display Disk Space Summary for All Mounted Filesystems:
df
Expected Output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 20511356 12456928 7042560 64% /
tmpfs 1024000 0 1024000 0% /dev/shm
- Filesystem: The device or partition name.
- 1K-blocks: Total space in 1KB blocks.
- Used: Space currently utilized.
- Available: Free space remaining.
- Use%: Percentage of used space.
- Mounted on: Directory where the filesystem is attached.
2. Human-Readable Format (Sizes in KB, MB, GB):
df -h
Expected Output:
/dev/sda1 20G 12G 6.7G 64% /
- The
-h
option converts units to human-readable formats.
3. Analyze a Specific Directory or Partition:
df /home
- This provides information only for the
/home
directory, useful for troubleshooting specific storage issues.
4. Include Filesystem Type Information:
df -T
Expected Output:
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 20G 12G 6.7G 64% /
- Displays the type of filesystem, such as ext4 or xfs.
5. Check Inode Usage (File System Metadata):
df -i
Expected Output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 1024000 50200 974800 5% /
- Useful when there is space available, but no more files can be created due to inode exhaustion.
3. Using the du Command – Detailed Disk Usage Analysis
The du
command provides detailed information about disk usage for files and directories.
Syntax:
du [OPTION] [FILE/DIR]
Examples:
1. View Disk Usage of a Directory:
du /var/log
Expected Output:
512 /var/log/mail
1024 /var/log/syslog
2048 /var/log
- Lists usage in KB for all subdirectories.
2. Human-Readable Sizes (MB, GB):
du -h /var/log
Expected Output:
512K /var/log/mail
1.0M /var/log/syslog
2.0M /var/log
3. Summarize Total Usage of a Directory:
du -sh /home/user
Expected Output:
arduino
Copy code
2.0M /home/user
-s
summarizes only the total usage of the directory.
4. Sort Files by Size – Find Large Files:
du -ah /home | sort -rh | head -10
- Displays the 10 largest files/directories in
/home
.
5. Exclude Specific File Types:
du -h --exclude='*.log' /var/log
- Ignores files ending in
.log
.
4. Combining df and du for Effective Monitoring
Examples
1. Check Overall Usage and Analyze Details:
df -h
du -sh /var/log
2. Find Large Files When Space Is Low:
df -h
du -ah /home | sort -rh | head -n 5
3. Redirect Output for Reports:
df -h > disk_report.txt
du -h /var/log >> disk_report.txt
5. Automating Disk Usage Monitoring with Cron Jobs
Schedule Disk Usage Checks with Cron
crontab -e
Add the following line:
0 8 * * * df -h > /home/user/disk_report.txt
- This command runs every day at 8 AM and saves the report in
/home/user/disk_report.txt
.
Key Takeaways
df
Command:
- Best for high-level monitoring of disk usage across mounted filesystems.
- Use
df -h
for human-readable output. - Check inode usage with
df -i
to detect metadata issues.
du
Command:
- Ideal for detailed analysis of directory and file sizes.
- Use
du -sh
to get the total size of a directory. - Combine with
sort
to find large files quickly.
Combining Commands:
- Use
df
to detect space issues anddu
to pinpoint causes.
Automation:
- Regular disk monitoring can be automated using cron jobs to avoid surprises.
Practical Use Cases:
- Cleaning up logs or cache files when space runs low.
- Preparing storage reports for audits.
- Monitoring disk usage growth trends.