How to Use the df Command in Linux

How to Use the df Command in Linux

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.

Click here to find the latest list of How to Use the df Command in Linux

Understanding the Basic Syntax

The basic syntax of the df command is as follows:

 df [options] [file or filesystem]        

Parameters:

  • file or filesystem: Specifies the particular file or file system whose disk usage is to be displayed.
  • options: Modifies the command output to display specific details.

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.

Also Read:- How to Move a File in Linux Like a Pro using mv Command

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:

  • Filesystem: Name of the storage device or partition.
  • Size: Total size of the file system.
  • Used: Space currently in use.
  • Avail: Available space for use.
  • Use%: Percentage of disk usage.
  • Mounted on: Directory where the file system is mounted.

Click here to find the latest list of How to Move a File in Linux Like a Pro using mv Command

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?

  • df -h displays disk space usage in KB, MB, or GB.
  • df -T adds a column showing the file system type (e.g., ext4, tmpfs).

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?

  • df reports disk space usage from the file system’s perspective.
  • du (disk usage) calculates space used by files in a directory.
  • Differences arise due to deleted but still open files or reserved space for system use.

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! ??

要查看或添加评论,请登录

Vivek Yadav的更多文章

  • How to Install Desktop (GUI) on Ubuntu Server

    How to Install Desktop (GUI) on Ubuntu Server

    Ubuntu Server is a lightweight, efficient, and powerful operating system designed primarily for server environments…

  • How to Install Ansible on Ubuntu

    How to Install Ansible on Ubuntu

    Ansible is a powerful automation tool that simplifies configuration management, application deployment, and task…

  • How to Reboot Ubuntu from Command Line

    How to Reboot Ubuntu from Command Line

    Rebooting an Ubuntu system from the command line is a common task for system administrators and users who manage…

  • How to Use the Whois Command on Ubuntu Linux

    How to Use the Whois Command on Ubuntu Linux

    The Whois command is a powerful tool used to retrieve domain and IP registration details from the internet. Whether you…

  • TCPDump Command in Linux

    TCPDump Command in Linux

    The command in Linux is a powerful network packet analyzer used for capturing and inspecting network traffic in real…

  • Command to See Who Is Logged Into a Computer

    Command to See Who Is Logged Into a Computer

    Knowing who is logged into a computer is crucial for security, troubleshooting, and system management. Whether you're a…

  • How to Install Apache on Ubuntu

    How to Install Apache on Ubuntu

    Apache is one of the most widely used open-source web servers in the world. It is a powerful, flexible, and highly…

  • Patch Management in Linux

    Patch Management in Linux

    Patch management in Linux is a crucial aspect of system administration, ensuring security, stability, and optimal…

  • iostat Command in Linux

    iostat Command in Linux

    The iostat command in Linux is a powerful utility that provides detailed statistics about CPU and disk I/O performance.…

  • Linux Video Capture Software

    Linux Video Capture Software

    Video capture software is an essential tool for Linux users who need to record their screen for tutorials, gaming…

社区洞察

其他会员也浏览了