df
Overview
The df
(disk free) command displays filesystem disk space usage information. It shows available and used space for mounted filesystems, essential for system monitoring and disk management.
Syntax
df [options] [filesystem...]
Common Options
Option | Description |
---|---|
-h |
Human-readable sizes (K, M, G) |
-H |
Human-readable with powers of 1000 |
-T |
Show filesystem type |
-i |
Show inode information |
-a |
Include dummy filesystems |
-l |
Local filesystems only |
-x type |
Exclude filesystem type |
-t type |
Include only filesystem type |
--total |
Display grand total |
--sync |
Sync before getting usage info |
Key Use Cases
- Check available disk space
- Monitor filesystem usage
- System health monitoring
- Capacity planning
- Troubleshoot disk full issues
Examples with Explanations
Example 1: Basic Usage
df -h
Shows disk usage in human-readable format
Example 2: Specific Filesystem
df -h /home
Shows usage for filesystem containing /home
Example 3: Show Filesystem Types
df -hT
Displays filesystem types along with usage
Example 4: Inode Information
df -hi
Shows inode usage instead of block usage
Understanding Output
Default columns: - Filesystem: Device or filesystem name - 1K-blocks: Total space in 1K blocks - Used: Used space - Available: Available space - Use%: Percentage used - Mounted on: Mount point
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 15G 4.2G 79% /
/dev/sda2 100G 45G 50G 48% /home
Common Usage Patterns
Check root filesystem:
df -h /
Monitor all local filesystems:
df -hl
Find full filesystems:
df -h | awk '$5 > 90 {print}'
Filesystem Types
Common filesystem types: - ext4: Linux native filesystem - xfs: High-performance filesystem - btrfs: Advanced Linux filesystem - ntfs: Windows filesystem - vfat: FAT32 filesystem - tmpfs: Temporary filesystem in RAM - nfs: Network filesystem
Advanced Usage
Exclude specific types:
df -h -x tmpfs -x devtmpfs
Show only specific type:
df -h -t ext4
Include totals:
df -h --total
Monitoring and Alerting
Check for full filesystems:
df -h | awk '$5 > 95 {print "WARNING: " $6 " is " $5 " full"}'
Monitor specific threshold:
USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//') if [ $USAGE -gt 80 ]; then echo "Root filesystem is ${USAGE}% full" fi
Performance Analysis
- Fast operation
- Reads filesystem metadata
- Minimal system impact
- Real-time information
- Good for automated monitoring
Best Practices
- Regular monitoring of disk space
- Set up alerts for high usage
- Use human-readable format
- Monitor both space and inodes
- Exclude irrelevant filesystems
Scripting Applications
Disk space monitoring:
#!/bin/bash THRESHOLD=90 df -h | awk -v thresh=$THRESHOLD ' NR>1 && $5+0 > thresh { print "WARNING: " $6 " is " $5 " full" }'
System health check:
check_disk_space() { echo "=== Disk Space Report ===" df -h | grep -vE '^Filesystem|tmpfs|cdrom' echo "" echo "=== Critical Usage (>90%) ===" df -h | awk '$5 > 90 {print $0}' }
Integration Examples
With cron for monitoring:
# Check disk space every hour 0 * * * * df -h | awk '$5 > 90 {print}' | mail -s "Disk Alert" admin@domain.com
System status dashboard:
echo "System Status - $(date)" echo "Disk Usage:" df -h | grep -v tmpfs echo "" echo "Inode Usage:" df -hi | awk '$5 > 80 {print}'
Inode Monitoring
Check inode usage:
df -hi
Find filesystems with high inode usage:
df -hi | awk '$5 > 80 {print "High inode usage: " $6 " (" $5 ")"}'
Monitor both space and inodes:
df -h && echo "--- Inodes ---" && df -hi
Troubleshooting
- Filesystem shows 100% but space available
- Inode exhaustion
- Stale NFS mounts
- Permission issues
- Filesystem corruption
Network Filesystems
Show only local filesystems:
df -hl
Include network filesystems:
df -h -t nfs -t cifs
Exclude network filesystems:
df -h -x nfs -x cifs
Output Formatting
Custom format:
df -h | awk '{printf "%-20s %8s %8s %8s %s\n", $1, $2, $3, $4, $6}'
CSV format:
df -h | awk 'NR>1 {printf "%s,%s,%s,%s,%s,%s\n", $1,$2,$3,$4,$5,$6}'
JSON format:
df -h | awk 'NR>1 {printf "{\"fs\":\"%s\",\"size\":\"%s\",\"used\":\"%s\",\"avail\":\"%s\",\"use\":\"%s\",\"mount\":\"%s\"}\n", $1,$2,$3,$4,$5,$6}'
Automation Examples
Cleanup trigger:
#!/bin/bash for fs in $(df -h | awk '$5 > 85 {print $6}'); do echo "Filesystem $fs is getting full" # Trigger cleanup scripts cleanup_logs.sh "$fs" done
Capacity planning:
df -h | awk ' NR>1 { gsub(/%/, "", $5) if ($5 > 70) print $6 " will need attention soon (" $5 "%)" }'