The df command is one of the most frequently used Linux command line utilities for checking disk space usage. As a Linux system administrator, having an in-depth understanding of df is critical for managing storage infrastructure.

This comprehensive guide will explore all facets of df usage – from basic syntax to advanced troubleshooting.

How Does df Work?

The df command provides a summary of unused and used disk space on all mounted filesystems in Linux by parsing two key sources of information:

/etc/mtab

This file contains a list of currently mounted filesystems. df relies on /etc/mtab to identify which disks/partitions to probe for statistics.

/sys/ Filesystems

This virtual filesystem provides detailed metrics on disk usage for each filesystem on a running Linux system. df queries files within /sys/ to build its output report.

So in summary, df combines current mount information with disk statistics pulled from /sys to produce a storage usage summary.

Display Disk Space for Mounted Disks

The most basic df invocation provides disk space statistics for mounted filesystems only:

$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             8004592       0   8004592   0% /dev
tmpfs            1628420    1948   1626472   1% /run
/dev/vda1      825564424 66011156 71436940   9% /
tmpfs           8142088       4   8142084   1% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs           8142088       0   8142088   0% /sys/fs/cgroup
/dev/vda15      1018084    56720    892456   6% /boot
tmpfs           1628416       4   1628412   1% /run/user/0

This output displays storage usage clearly showing the filesystem device name, 1K-block totals (KB size), used vs available blocks, percentage used and where each filesystem is mounted.

Show All Filesystems with -a

The -a flag tells df to show information on all filesystems, even if not mounted:

$ df -a 
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             8004592       0   8004592   0% /dev  
tmpfs            1628420    1948   1626472   1% /run
/dev/vda1      825564424 66011156 71436940   9% /
tmpfs           8142088       4   8142084   1% /dev/shm
tmpfs               5120       4      5116   1% /run/lock  
tmpfs           8142088       0   8142088   0% /sys/fs/cgroup
/dev/vda15      1018084    56720    892456   6% /boot    
tmpfs           1628416       4   1628412   1% /run/user/0

This can help identify disk partitions that are unmounted or offline. The output includes dummy and duplicate filesystems also.

Show Totals with –total

Adding the –total flag provides a summary line showing combined totals for all disks:

$ df --total
Filesystem     1K-blocks    Used Available Use% Mounted on
udev             8004592       0   8004592   0% /dev
tmpfs            1628420    1948   1626472   1% /run
/dev/vda1      825564424 66011156 71436940   9% / 
tmpfs           8142088       4   8142084   1% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs           8142088       0   8142088   0% /sys/fs/cgroup  
/dev/vda15      1018084    56720    892456   6% /boot
tmpfs           1628416       4   1628412   1% /run/user/0
total         843573232 67387840 71644176   9% /

The total summary quantifies disk space and utilization for all mounted filesystems.

Show Human-Readable Sizes with -h

The -h flag displays disk space in human-readable format (KB, MB, GB), making it easier to parse:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev             7.7G     0  7.7G   0% /dev
tmpfs           1.6G  1.9M  1.6G   1% /run
/dev/vda1        79G   64G   15G  81% /
tmpfs           7.9G  4.0K  7.9G   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/vda15      996M  554M  442M  56% /boot
tmpfs           1.6G  4.0K  1.6G   1% /run/user/0

Human sizes make it easier to process disk usage at a glance.

Select Specific Units with kb, mb, gb

To specify the units for disk space amounts, use kb, mb or gb arguments:

$ df -k
$ df -m
$ df -g  

For example, to show results in megabytes:

$ df -m
Filesystem    1M-blocks  Used Available Use% Mounted on
udev              78408     0     78408   0% /dev  
tmpfs             15948    14     15935   1% /run
/dev/vda1       8078716 25244   758252   1% /      
tmpfs             79362    12     79350   1% /dev/shm
tmpfs                14     12        14   0% /run/lock
tmpfs             79362      0     79362   0% /sys/fs/cgroup    
/dev/vda15         988    552       436  56% /boot
tmpfs             15948     4     15943   1% /run/user/0

Analyze Specific Filesystem Types

The -t flag shows information for only a specified filesystem type, as denoted by /etc/fstab, for example:

$ df -t ext4
$ df -t xfs

This can help analyze disk space specifically on the required filesystems.

Exclude Specified Filesystems

To omit certain filesystems from the df output, use the -x flag:

$ df -x tmpfs

This is useful for focusing your disk report only on physical storage volumes.

Show Inode Information with -i

The -i flag displays additional details about inodes – the underlying filesystem structure that manages individual files:

$ df -i
Filesystem      Inodes   IUsed    IFree IUse% Mounted on  
udev            2006148      561  2005587    1% /dev         
tmpfs           2035604     1490  2034114    1% /run     
/dev/vda1     52426752 3787393 48639589    8% /
tmpfs          2035604       19 2035585    1% /dev/shm  
tmpfs          2035604        5 2035599    1% /run/lock  
tmpfs          2035604        2 2035602    1% /sys/fs/cgroup
/dev/vda15     261273     7150   254123    3% /boot   
tmpfs          2035604       20 2035584    1% /run/user/0

Monitoring inodes helps identify potential inode depletion issues.

Troubleshooting df Errors

Some common errors with df and their usual causes:

Stale File Handle – Filesystem was unmounted/removed

Transport Endpoint Not Connected – Network fileshare unavailable

Timeout Acquiring Fileset Information – Underlying storage is inaccessible

Debug tips for above errors:

  • Check filesystem mounts with mount command
  • Verify disk connectivity and configuration
  • Test network connectivity to remote filesystems

Best Practices for Using df

To leverage df most effectively:

  • Set up thresholds (% used) for alerting
  • Schedule periodic df checks with cron
  • Parse df output to feed monitoring tools
  • Baseline normal usage with historical df data
  • Script df to run checks autonomously

For example, a simple baseline monitoring script:

#!/bin/bash

baseline=/var/opt/diskspace/baseline.txt 

df -h > tempdiskuse.txt

percentused=$(sed -n ‘s/\S*% \/$/\1/p‘ tempdiskuse.txt)
echo "$(date) percentused = $percentused" >> $baseline

if [[ $percentused > 70 ]]; then
   echo "Alert: Disk space crossed 70%" | mail -s "Disk Space Alert" admin@example.com
fi  

This runs df periodically, storing usage % over time and alerting when thresholds crossed.

Visualizing Disk Usage

Charts and data visualizations are useful for identifying disk usage growth patterns over longer durations:

Tools like pydf, gdmap and durep can be used to generate graphs from df statistics.

Comparing df to Related Tools

du – disk usage summary per directory rather than whole filesystem

find – find files matching criteria rather than overall usage

ncdu – interactive ncurses-based UI for drilling into disk usage

pydf – Python module to plot interactive graphs from df stats

So while df provides a filesystem/disk level overview, utilities like du and ncdu complement it by letting you analyze disk space usage at finer granularity.

Under the Hood: df Implementations

Modern Linux distributions implement df via calls to:

/proc/self/mountinfo – gathers mounted filesystem data

/sys/block filestat data – provides underlying device usage statistics

Earlier Linux systems relied instead on:

/etc/mtab entries – to get mountpoints

sys_statvfs system call – pull filesystem metrics

So while df output remains consistent in all Linux variants, the back-end implementation differs. Similarly df functionality varies in other UNIX systems – FreeBSD, OpenBSD, Solaris, HP-UX etc. But POSIX standards ensure baseline compatibility.

Summary

The humble but essential df command offers valuable insights into disk usage across any Linux environment. Combining its capabilities for customizable output with monitoring best practices allows effectively managing storage growth and preventing outages. This guide has covered df from basic to advanced usage – enabling you to squeeze the most value from this ubiquitous Linux administrative tool.

Similar Posts

Leave a Reply

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