How to Use the df Command in Linux
Vivek Yadav
Full Stack Developer | JavaScript & Python | Passionate about Web Development
The df command in Linux is a powerful utility used to check disk space usage on a system. It provides a quick overview of available and used space on all mounted file systems. Whether you are a system administrator, developer, or Linux enthusiast, understanding how to use the df command in Linux is essential for monitoring storage and ensuring smooth system operations.
In this guide, we will explore the syntax, basic and advanced usage, key options, and common troubleshooting techniques for the df command. Additionally, we will address frequently asked questions to provide a comprehensive understanding of this essential tool.
Understanding the Basic Syntax
The basic syntax of the df command is as follows:
df [options] [file or filesystem]
Parameters:
Example Usage:
To check disk usage in a human-readable format, use:
df -h
This command presents output in a user-friendly format with sizes in KB, MB, GB, or TB instead of default block sizes.
How to Check Disk Space Using the df Command
Checking All Mounted File Systems
To view disk space usage of all mounted file systems, simply run:
df
Checking a Specific File System
To check disk space usage of a specific directory or file system, specify its mount point. For example, to check /home:
df /home
Displaying Sizes in Human-Readable Format
To display file system sizes in KB, MB, or GB instead of blocks:
df -h
Understanding df Command Output
Running df -h typically displays output like this:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 30G 40% /
tmpfs 500M 100K 499M 1% /run
devtmpfs 1.9G 0 1.9G 0% /dev
Explanation of Columns:
Using Options with the df Command
The df command provides several options to customize the output:
-h: Human-Readable Format
df -h
Displays sizes in KB, MB, GB, or TB for better readability.
-T: Show File System Type
领英推荐
df -T
Displays an additional column showing the type of file system (e.g., ext4, xfs, tmpfs).
-i: Show Inode Usage
df -i
Displays inode usage instead of block usage, useful for diagnosing inode exhaustion.
-P: POSIX Format
df -P
Displays output in a standardized, more script-friendly format.
Advanced df Command Usage
Checking Disk Space for Specific File Types
Use the df -t option to filter file systems of a specific type:
df -t ext4
Using df with grep for Filtering Results
To check only the root partition:
df -h | grep '/$'
Automating Disk Usage Monitoring with Scripts
Create a script to check disk space and alert if usage exceeds a threshold:
#!/bin/bash
THRESHOLD=80
USAGE=$(df -h | awk 'NR>1 {print $5 " " $6}' | sed 's/%//g')
while read output; do
use=$(echo $output | awk '{print $1}')
partition=$(echo $output | awk '{print $2}')
if [ $use -ge $THRESHOLD ]; then
echo "Warning: $partition usage is at ${use}%!"
fi
done <<< "$USAGE"
Save this script as check_disk_usage.sh and execute it periodically to monitor disk space.
FAQs
What is the df command used for in Linux?
The df command is used to check disk space usage on Linux systems. It displays the available and used space of all mounted file systems.
How do I check free space using the df command?
Use:
df -h
This command shows available space in a human-readable format.
What is the difference between df -h and df -T?
How can I check inode usage with df?
To check inode usage, use:
df -i
This is useful when dealing with a high number of small files that may exhaust available inodes before running out of disk space.
Why does df show different values from du?
Conclusion
The df command in Linux is an essential tool for monitoring and managing disk space usage. Whether you need to check available storage, monitor inode usage, or filter results, understanding how to use df effectively can help you maintain a healthy system.
Now that you have a complete guide, try these commands on your system and explore the various options available. If you have any questions or tips, feel free to share them below! ??