BLOG POSTS
    MangoHost Blog / df – Display disk filesystem information: Usage, Scripts for free disk space monitoring
df – Display disk filesystem information: Usage, Scripts for free disk space monitoring

df – Display disk filesystem information: Usage, Scripts for free disk space monitoring

Introduction

The df command, short for “Display disk filesystem information”, is a Linux utility that provides information about the disk space usage on a file system. It is commonly used to check the available disk space on a system, monitor disk usage, and identify potential storage issues.

The df command is a part of the GNU Core Utilities package, which is a collection of essential command-line tools for Unix-like operating systems. It is written in the C programming language and is available for various Unix-like operating systems, including Linux, macOS, and BSD.

The official page for the df command can be found on the GNU website: https://www.gnu.org/software/coreutils/manual/html_node/df-invocation.html

Installation

The df command is typically pre-installed on most Linux distributions and does not require any additional installation steps. However, if it is not available on your system, you can install it using the package manager specific to your distribution.

Debian/Ubuntu

To install the df command on Debian or Ubuntu, open a terminal and run the following command:

sudo apt-get install coreutils

Red Hat/Fedora

To install the df command on Red Hat or Fedora, open a terminal and run the following command:

sudo dnf install coreutils

CentOS

To install the df command on CentOS, open a terminal and run the following command:

sudo yum install coreutils

Usage

The df command provides various options and arguments to customize its output and behavior. Here are some commonly used commands and their descriptions:

Basic Usage

To display the disk space usage of all mounted file systems, use the following command:

df

This will show the total, used, and available disk space for each file system.

Display in Human-Readable Format

To display the disk space usage in a more human-readable format, use the -h option:

df -h

This will show the disk space in sizes like “1K”, “1M”, “1G” instead of the default 1,024-byte blocks.

Display File System Type

To display the file system type along with the disk space usage, use the -T option:

df -T

This will show the file system type in the “Type” column.

Display Inodes

To display the number of used and available inodes (index nodes) instead of disk space, use the -i option:

df -i

This can be useful for checking the inode usage on a file system.

Filter File Systems

To filter the output to display only specific file systems, you can specify the file system(s) as arguments to the df command. For example, to display the disk space usage of the root file system, use the following command:

df /

This will show the disk space usage of the root file system only.

Similar Commands

There are several other commands and utilities that provide similar functionality to the df command:

  • du: The du command is used to estimate file and directory space usage. It provides more detailed information about the disk space usage of individual files and directories.
  • lsblk: The lsblk command is used to list information about block devices, including disk partitions and their mount points.
  • mount: The mount command is used to mount file systems and display information about currently mounted file systems.

Automation Scripts

Here are three example scripts that demonstrate the usage of the df command in automation:

Script 1: Disk Space Monitoring

This script monitors the disk space usage of a specific file system and sends an email notification if the usage exceeds a certain threshold.

#!/bin/bash

# File system to monitor
FILE_SYSTEM="/dev/sda1"

# Threshold in percentage
THRESHOLD=90

# Get the disk space usage
USAGE=$(df -h --output=pcent "$FILE_SYSTEM" | tail -n 1 | tr -d '%')

# Check if the usage exceeds the threshold
if [ "$USAGE" -gt "$THRESHOLD" ]; then
# Send an email notification
echo "Disk space usage on $FILE_SYSTEM exceeded $THRESHOLD%" | mail -s "Disk Space Alert" admin@example.com
fi

Script 2: Disk Space Cleanup

This script identifies and deletes old log files to free up disk space on a specific file system.

#!/bin/bash

# File system to clean up
FILE_SYSTEM="/var/log"

# Maximum age of log files in days
MAX_AGE=7

# Find and delete old log files
find "$FILE_SYSTEM" -type f -name "*.log" -mtime +$MAX_AGE -delete

Script 3: Disk Space Reporting

This script generates a report of the disk space usage for all file systems and saves it to a file.

#!/bin/bash

# Output file
OUTPUT_FILE="disk_space_report.txt"

# Generate the disk space report
df -h > "$OUTPUT_FILE"

List of Functions and Constants

Here is a table listing some of the functions (commands) and constants available in the df command:

Function/Constant Description
df Display disk space usage
-h Display sizes in human-readable format
-T Display file system type
-i Display inodes instead of disk space
du Estimate file and directory space usage
lsblk List information about block devices
mount Mount file systems and display information about mounted file systems

Conclusion

The df command is a powerful tool for monitoring and managing disk space usage on Linux systems. It provides essential information about file system usage and helps identify potential storage issues. The df command is widely used by system administrators, DevOps engineers, and anyone responsible for managing disk space on Linux servers. It can be used in various automation scripts to monitor disk space, clean up old files, and generate reports. Overall, the df command is an essential utility for maintaining optimal disk space utilization in real-world scenarios.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked