Introduction
The df command (short for “disk free”) is one of the most essential utilities in the Linux system administrator’s toolkit. Whether you’re managing a personal Linux system or administering production servers, understanding how to use df effectively is crucial for maintaining healthy storage systems and preventing disk-related issues.
This comprehensive guide will walk you through everything you need to know about the df command, from basic usage to advanced monitoring techniques. You’ll learn how to interpret df output, use various options to customize the display, and integrate df into your system monitoring workflows.
Understanding the df Command
The df command displays information about disk space usage on file systems. When executed without any arguments, it shows information about all mounted file systems, including their total size, used space, available space, and mount points.
Basic Syntax
df [OPTIONS] [FILESYSTEM|FILE]
The command accepts optional arguments that modify its behavior and output format. Without any arguments, df displays information about all currently mounted file systems.
Why df Matters
Disk space monitoring is critical for several reasons:
- Preventing System Crashes: When disk space runs out, applications cannot write new data, potentially causing system failures
- Capacity Planning: Understanding usage patterns helps in planning storage upgrades
- Identifying Issues: Unusual disk usage can indicate problems like log file growth or malware activity
- Resource Optimization: Regular monitoring helps identify opportunities to free up space
Basic df Usage
Simple Output
The most basic way to use df is without any arguments:
df
This displays output in 1K blocks by default. The columns show:
- Filesystem: The device or mount point
- 1K-blocks: Total size in 1-kilobyte blocks
- Used: Amount of space used
- Available: Amount of space available
- Use%: Percentage of space used
- Mounted on: The mount point
Human-Readable Output
For easier reading, use the -h flag (human-readable):
df -h
This displays sizes in appropriate units (KB, MB, GB, TB) rather than raw blocks:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 45G 55G 45% /
tmpfs 1.6G 0 1.6G 0% /dev/shm
/dev/sdb1 500G 350G 150G 70% /data
Displaying Filesystem Type
To see the type of each filesystem, use the -T option:
df -hT
Output includes an additional column showing the filesystem type:
Filesystem Type Size Used Avail Use% Mounted on
ext4 ext4 100G 45G 55G 45% /
xfs xfs 500G 350G 150G 70% /data
tmpfs tmpfs 1.6G 0 1.6G 0% /dev/shm
Common df Options
The -h Option (Human Readable)
As shown above, -h makes output human-friendly:
df -h
You can also combine it with other options:
df -hT
The -i Option (Inodes)
While disk space is commonly monitored, inode exhaustion is also a serious issue:
df -i
Inodes are data structures that store information about files. Each file requires at least one inode, and running out of inodes can prevent file creation even when disk space is available.
The -a Option (All Filesystems)
Include pseudo, duplicate, and inaccessible file systems:
df -ah
The -x Option (Exclude Filesystems)
Exclude specific filesystem types:
df -h -x tmpfs -x devtmpfs
This is useful when you only want to see physical disk usage.
The -t Option (Include Specific Filesystems)
Only show filesystems of a specific type:
df -h -t ext4
df -h -t xfs
Practical Examples
Checking Root Partition Usage
To check the root partition specifically:
df -h /
This shows only the filesystem containing the root directory.
Checking Specific Mount Points
Monitor specific directories:
df -h /home
df -h /var
df -h /tmp
Extracting Specific Information
To get just the usage percentage for the root partition:
df -h | awk 'NR==2' | awk '{print $5}'
Breaking this down:
NR==2: Selects the second line (first line is headers, assuming root is second)$5: Prints the fifth column (Use%)
To remove the percentage sign:
df -h | awk 'NR==2' | awk '{print $5}' | tr -d '%'
Getting Available Space Only
To extract just the available space in GB:
df -h / | awk 'NR==2 {print $4}'
Sorting by Usage
To sort filesystems by usage percentage:
df -h | sort -k5 -hr
This sorts by the fifth column (Use%) in descending order.
Understanding df Output Columns
Filesystem Column
The first column identifies the source of each filesystem:
/dev/sd*: Physical SATA/SAS disks/dev/nvme*: NVMe SSDs/dev/mapper/*: Logical volumes (LVM)tmpfs: Temporary filesystems in memorydevtmpfs: Device filesystemsoverlay: Container filesystems
Size, Used, and Available Columns
These columns show:
- Size: Total capacity of the filesystem
- Used: Space currently consumed
- Available: Space remaining for use
Note that Linux reserves a portion (typically 5%) of disk space for the root user to ensure system recovery is possible even when regular users fill the disk.
Use% Column
The usage percentage shows how full each filesystem is:
- 0-60%: Normal operational range
- 60-80%: Monitor closely, plan for expansion
- 80-90%: Action required, begin cleanup
- 90-100%: Critical, immediate action needed
Mounted On Column
This shows where each filesystem is attached in the directory tree:
/: Root filesystem/boot: Boot files/home: User directories/var: Variable data (logs, caches)/tmp: Temporary files
Advanced Monitoring Techniques
Scripting with df
Integrate df into monitoring scripts:
#!/bin/bash
# Check disk usage and alert if above threshold
THRESHOLD=80
df -h | grep -vE '^Filesystem|tmpfs|devtmpfs' | awk '{print $1, $5}' | while read filesystem usage; do
usage_pct=${usage%\%}
if [ "$usage_pct" -gt "$THRESHOLD" ]; then
echo "Alert: $filesystem is at ${usage} usage"
fi
done
Cron-Based Monitoring
Set up automated checks in crontab:
0 0 * * * /path/to/disk-check-script.sh
Real-Time Monitoring
Combine df with watch for continuous monitoring:
watch -n 5 df -h
This updates the display every 5 seconds.
Checking Multiple Servers
Use SSH to check disk space on multiple servers:
for server in server1 server2 server3; do
echo "=== $server ==="
ssh $server "df -h | grep -vE '^Filesystem|tmpfs|devtmpfs'"
done
Troubleshooting Disk Space Issues
Identifying Large Directories
When disk space is low, find what’s using the most space:
du -sh /*
du -sh /var/*
Finding Large Files
Find files larger than a certain size:
find / -type f -size +100M -exec ls -lh {} \;
Checking for Deleted but Open Files
Sometimes files are deleted but still consuming space because processes still have them open:
lsof +L1
Log File Management
Common space consumers include log files:
ls -lah /var/log/
journalctl --disk-usage
df vs. du: Understanding the Difference
While df shows filesystem-level information, du (disk usage) shows directory-level usage:
- df: Shows available space on mounted filesystems
- du: Shows how much space specific directories or files use
Use both commands together:
df -h # Check overall space
du -sh /var/log # Check specific directory
Related Commands
lsblk
List block devices in a tree format:
lsblk
du
Show directory space usage:
du -sh /path/to/directory
fdisk
Partition table manipulation:
sudo fdisk -l
parted
Advanced partition management:
sudo parted -l
Best Practices
- Regular Monitoring: Check disk space regularly, not just when problems occur
- Set Up Alerts: Use scripts to alert when thresholds are exceeded
- Plan Capacity: Monitor trends to anticipate when you’ll need more storage
- Clean Up: Regularly clean temporary files, old logs, and unused packages
- Understand Patterns: Know which directories grow over time
- Document: Keep records of disk usage over time for trend analysis
Common Use Cases
Server Administration
System administrators use df constantly:
# Quick check
df -h
# Check specific mount points
df -h / /var /home
# Exclude memory filesystems
df -h -x tmpfs -x devtmpfs
Development Environments
Developers often run into disk issues with containers and build artifacts:
# Check Docker volumes
df -h /var/lib/docker
# Check build directories
df -h ~/projects
Personal Computers
Desktop users should monitor:
/home(user files)/(system files)/tmp(temporary files)
Conclusion
The df command is an indispensable tool for anyone working with Linux systems. By understanding its various options and output formats, you can effectively monitor disk space, prevent storage-related issues, and maintain healthy systems.
Remember to integrate disk monitoring into your regular system administration routine, and consider setting up automated alerts to catch potential issues before they become critical problems.
Comments