12 Useful “df” Commands to Check Disk Space in Linux

Introduction

Managing disk space efficiently is crucial for maintaining the performance and stability of your Linux system. The “df” command, short for “disk free,” is a versatile tool that allows you to monitor disk usage and available space on your storage devices. In this extensive guide, we will delve into twelve valuable “df” commands that will help you effectively manage your disk space in Linux. These commands are presented in easy-to-understand language, making them accessible to both beginners and experienced users. Additionally, we will optimize this guide for search engines, ensuring that it is readily discoverable by those seeking solutions for disk space management.

1. What is the “df” Command?

The “df” command, standing for “disk free,” is a command-line utility in Linux used to display information about the disk space usage on storage devices, including hard drives, partitions, and mounted filesystems. It provides vital insights into the status of your disk space, helping you make informed decisions about storage management.

2. Basic Usage of “df”

To get started with the “df” command, open your terminal or command prompt. The basic syntax of “df” is as follows:

df [options] [filesystem or directory]
  • options: These are optional parameters that modify the behavior of the “df” command.
  • filesystem or directory: This is the filesystem or directory for which you want to check disk space. If not specified, “df” will display information for all mounted filesystems.

3. Displaying Filesystem Information

One of the fundamental uses of the “df” command is to display information about mounted filesystems. Simply running “df” without any options or arguments will provide an overview of all mounted filesystems along with their disk space usage. For example:

df

This command will list all mounted filesystems, including their device name, total space, used space, available space, and mount point.

4. Displaying Disk Space Usage in Human-Readable Format

By default, “df” displays disk space usage in kilobytes (KB), which may not be very user-friendly. To make the output more human-readable, you can use the -h option, which stands for “human-readable.” For example:

df -h

This command will present the disk space usage in a more understandable format, using units like kilobytes (KB), megabytes (MB), and gigabytes (GB).

5. Sorting and Displaying Disk Space Usage

To sort the disk space usage results in descending order and display the filesystem with the most space used at the top, you can combine “df” with other commands. For instance, to list filesystems in descending order of usage, use this command:

df -h | sort -rh

This command will list filesystems from the one with the highest disk space usage to the lowest.

6. Excluding Specific Filesystems

There may be instances where you want to exclude specific filesystems from the “df” command’s output. You can achieve this by using the --exclude option. For example, if you want to view all mounted filesystems except the root filesystem, use the following command:

df -h --exclude=/ 

This command will display information for all mounted filesystems except the root (“/”) filesystem.

7. Displaying Inodes Usage

In addition to checking disk space usage, “df” can also display information about inodes usage. Inodes are data structures used to store metadata about files on a filesystem. To view inodes usage along with disk space usage, use the -i option. For example:

df -hi

This command will display information about inodes usage for all mounted filesystems.

8. Checking Specific Mount Points

To check the disk space usage of a specific mount point, you can provide the mount point’s path as an argument to the “df” command. For example, to check the disk space usage of the “/home” directory, use this command:

df -h /home

This command will display information about the disk space usage of the “/home” directory.

9. Displaying the Total Disk Space Usage

If you want to find the total disk space usage across all mounted filesystems, you can use the --total option. For example:

df -h --total

This command will display the disk space usage for each filesystem and provide a final total sum of disk space usage.

10. Monitoring Disk Space Usage Over Time

To monitor disk space usage over time, you can create a shell script that runs the “df” command at specified intervals and logs the results. This can be helpful for tracking changes in disk space usage and identifying potential issues. Here’s a simple example of a shell script that logs disk space usage to a file:

#!/bin/bash

while true
do
  df -h >> disk_usage.log
  sleep 3600  # Sleep for 1 hour (adjust as needed)
done

This script runs the “df” command every hour and appends the results to a file named “disk_usage.log.”

11. Understanding Disk Space Reservations

Linux filesystems often reserve a portion of disk space for system use, which is not included in the reported free space. To understand the actual free space available to non-root users, you can use the -x option. For example:

df -hx tmpfs

This command will display disk space usage, excluding filesystems of type “tmpfs,” which is often used for system-related purposes.

12. Using “df” with Shell Scripts

You can create custom shell scripts that utilize “df” to automate disk space monitoring and alerting. These scripts can be set up to send email notifications or generate alerts when disk space crosses a certain threshold. This proactive approach helps you prevent potential issues caused by low disk space. Here’s a basic example of a shell script that checks disk space and sends an email alert if it’s below a specified threshold:

#!/bin/bash

# Set the threshold in gigabytes
threshold=10

# Get the available disk space in gigabytes
available=$(df -h / | awk 'NR==2 {print $4}' | sed 's/G//')

# Check if available space is below the threshold
if [ "$available" -lt "$threshold" ]; then
  # Send an email alert
  echo "Warning: Low disk space on $(hostname)" | mail -s "Disk Space Alert" your@email.com
fi

This script checks the available disk space on

the root filesystem (“/”) and sends an email alert if it’s below the specified threshold (in this case, 10 gigabytes).

Conclusion

You’ve now gained a comprehensive understanding of the “df” command and its myriad applications in disk space management within a Linux environment. These twelve “df” commands enable you to effectively monitor, analyze, and optimize your disk space, ensuring your system runs smoothly and efficiently.

By mastering these “df” commands, you can make informed decisions about storage allocation, proactively address low disk space issues, and maintain the health and performance of your Linux system. Regularly monitoring disk space is a critical aspect of system maintenance, and with the knowledge gained from this guide, you are well-equipped to tackle any disk space management challenges that may arise.

In conclusion, effective disk space management is a vital component of maintaining a healthy and efficient Linux system. With the insights and tools provided in this guide, you are empowered to take control of your disk space and ensure the smooth operation of your Linux environment. Happy disk space management!

Leave a Comment