df Command: Tutorial & Examples

Display information about free disk space

The df command is a Linux utility that displays the amount of available disk space for file systems on which the system is installed. When you run the df command without any options, it shows the amount of free and used disk space on all mounted file systems.

How df works

The df command gathers information from the kernel and displays it in a human-readable format. It reads the /proc/mounts file to identify mounted file systems and their types, then queries each file system to retrieve its disk usage statistics. This information is obtained through system calls that interact with the kernel's virtual file system.

What df does

The df command provides several key pieces of information for each mounted file system:

  • Filesystem: The name of the file system.
  • 1K-blocks: The total size of the file system in 1K blocks.
  • Used: The amount of space used.
  • Available: The amount of space available for use.
  • Use%: The percentage of space used.
  • Mounted on: The directory where the file system is mounted.

Why df is important

Understanding disk space usage is critical for system administrators. The df command allows users to monitor disk usage, which helps prevent issues such as disk full errors that can disrupt services or applications. Regular monitoring can help in capacity planning and avoiding unexpected outages.

How to use df

To use the df command, simply type df in your terminal. For example:

df

This will yield an output similar to:

Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1       10240000  2048000   8192000  20% /

You can also use the command with options to customize the output for specific needs.

Common command line parameters

The df command comes with various options to customize its output:

  • -h: Display sizes in a human-readable format (e.g., MB, GB).

    df -h
    
  • -T: Show the file system type.

    df -T
    
  • -a: Include dummy file systems.

    df -a
    
  • -i: Display inode information instead of block usage.

    df -i
    

Potential problems and pitfalls

When using the df command, users might face the following issues:

  • Incorrect information: If a file system is not mounted properly, it may not appear in the df output. Ensure all file systems are correctly mounted.

  • Permission errors: Running df as a non-privileged user may restrict visibility to certain file systems. Running the command with sudo can help:

    sudo df -h
    

Technical background

The df command interacts with the kernel's virtual file system to obtain information about mounted file systems. The kernel maintains metadata about disk usage, which df queries to present an accurate picture of available and used space. Understanding the underlying mechanics can help troubleshoot issues related to disk management.

Real-world use cases

The df command is commonly used in the following scenarios:

  • Monitoring disk space: System administrators can schedule the df command to run at regular intervals to monitor disk usage.

    df -h >> disk_space.log
    
  • Automated scripts: It can be integrated into scripts that alert administrators when disk space crosses a certain threshold. For example, a script could be structured as follows:

    if [ $(df / | grep / | awk '{ print $5 }' | sed 's/%//g') -gt 90 ]; then
        echo "Disk space is above 90%!" | mail -s "Disk Alert" admin@example.com
    fi
    

Hacks and tricks

  • Use df -hT to view both the size and type of each file system in a single command.

    df -hT
    
  • Combine df with grep to filter output for specific file systems:

    df -h | grep '/dev/sda1'
    

Tips and best practices

  • Use -h with the df command for more understandable output.
  • Regularly check disk usage to avoid running into issues with disk full scenarios.
  • Consider logging the output to a file for historical reference.
  • Monitor disk usage in scripts to automate alerts for disk space thresholds.
  • Schedule regular checks using cron for proactive monitoring.

See also

The text above is licensed under CC BY-SA 4.0 CC BY SA