How to use df command in Linux / Unix {with examples}

We use the df command to find total disk space and available space on a Linux / Unix file system. Let us see how to use the df command to view the amount of free space left on your Linux, macOS, *BSD and Unix file system. We will also explain how to automate disk space monitoring and send an email alert or push notification to your mobile device.

The df is an essential tool for a developer as it reports the amount of disk space used and available on file Linux or Unix filesystem.

command details
DescriptionReport free disk space
CategoryDisk space analyzers
DifficultyEasy
Root privilegesNo
Est. reading time9 minutes
Table of contents

What is the df command?

The df is an acronym for disk free.

The df command tells the amount of space used and available on the file system. A filesystem is nothing but a hierarchy of directories. For instance, figure 01 shows everything mounted on my Linux laptop in the root directory (/), which is the top directory in the hierarchy of directories using the tree command. Some filesystems such as /mirror-nixcraft-m6700 or /media are physically located on different partitions/USB disks or another computer on a network.

Figure 01: A hierarchy of directories with / (root) as the top directory

Purpose

Use the df command for displaying available space on a file system:

  1. View used space.
  2. See free disk space.
  3. Show what filesystems are mounted.
  4. Find out if disk capacity has been reached on a partition or other device networked devices.
  5. List the number of inodes still available for Linux and Unix box
  6. Discover if there is sufficient space on a given partition so that developers or sysadmins can install/upgrade apps
  7. Send email alerts or text messages using a shell script or Perl script when you run out of disk space.

Syntax

The basic syntax for df command is strethforward:
df
df /dir
df [option] [/dev/DEVICE]

Using the df command

To see information about all mounted Linux or Unix file systems, enter:
df

The output might be slightly different on the GNU/Linux version of df:

Filesystem           1K-blocks      Used Available Use% Mounted on
devtmpfs                 10240         0     10240   0% /dev
shm                    3702044         0   3702044   0% /dev/shm
rpool/ROOT/alpine    471240576  96435456 374805120  20% /
tmpfs                   740412       220    740192   0% /run
/dev/sda1                91869     28015     56686  33% /boot


The df command and space can have the following info for each device name:

  • Filesystem : The name of your file system/device.
  • 1K-blocks : The size in one-kilobyte blocks.
  • Used : Total amount of space allocated and used by existing files in the file system (FS).
  • Avail (Used Available) : Total amount of space available (free space) in file system for the creation of new files.
  • Capacity (Use%) : The percentage of blocks used by the FS.
  • Mounted on : The mount point is the directory in which it is mounted.

Please note that some advanced file systems and df versions can also report space reserved by the system.

Examples

The df command has many more options. Let us explore the most commonly used options.

File systems info

To display information about /boot or /rsnapshot file system, run:
df /boot
df /rsnapshot

Here is what I see:

Filesystem       1K-blocks     Used      Avail Capacity  Mounted on
zroot/rsnapshot 3133726266 83069592 3050656673     3%    /rsnapshot

We can display information in various block format. For instance see 1M-byte blocks, run:
df -m
df -m /boot

For 1 Gibibyte blocks, run:
df -g
df -g /var

We can explicitly use 512 byte blocks:
df -b
df -b /usr

Pass the -k for 1024-byte blocks (default):
df -k
df -k zroot/var/log

We can specify ZFS device such as zroot/var/log or /dev/md0 (Linux RAID) and /dev/sdb1 (Linux partition) too apart from FS mount point such as /aws_efs:
df /aws_efs
df zroot/ROOT/default
df /dev/md0
df -h /dev/sdb1

Human readable output

Perhaps most important is the “Human readable” output by passing the -h option. It uses unit suffixes such as Byte, Kibibyte, Mebibyte, Gibibyte, Tebibyte, and Pebibyte (based on powers of 1024). You get the ease of reading as df reduces the number of digits to four or fewer. Please note the -H is same as -h but based on powers of 1000:
df -h # lowercase 'h'
df -H # UPPERCASE 'H'
df -H zroot/tmp
df -H /dev/md0

Filesystem                  Size    Used   Avail Capacity  Mounted on
zroot/ROOT/default          3.3T    129G    3.1T     4%    /
devfs                       1.0k    1.0k      0B   100%    /dev
zroot/tmp                   3.1T    164k    3.1T     0%    /tmp
zroot                       3.1T    131k    3.1T     0%    /zroot
zroot/rsnapshot             3.2T     85G    3.1T     3%    /rsnapshot
zroot/backuplxdimages       3.5T    350G    3.1T    10%    /backuplxdimages
zroot/jails                 3.1T    131k    3.1T     0%    /jails
zroot/usr/home              3.2T     33G    3.1T     1%    /usr/home
zroot/var/crash             3.1T    638M    3.1T     0%    /var/crash
zroot/var/mail              3.1T    257k    3.1T     0%    /var/mail
zroot/var/audit             3.1T    131k    3.1T     0%    /var/audit
zroot/var/log               3.1T    2.6M    3.1T     0%    /var/log
zroot/usr/ports             3.1T    1.0G    3.1T     0%    /usr/ports
zroot/var/tmp               3.1T    131k    3.1T     0%    /var/tmp
zroot/usr/src               3.1T    131k    3.1T     0%    /usr/src
zroot/jails/dnscrypt        3.1T    887M    3.1T     0%    /jails/dnscrypt
zroot/jails/fullbasejail    3.1T    692M    3.1T     0%    /jails/fullbasejail
devfs                       1.0k    1.0k      0B   100%    /jails/dnscrypt/dev

Print filesystem type

Want to include file system type in df command output? Try passing the -T option:
df -T
df -T -H
df -T -H zroot/usr/ports
df -T -H /dev/md0

The following outputs indicate that /dev/md0 formatted as ext4:

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/md0       ext4  3.9G  350M  3.4G  10% /boot

Another output indicating vfat FS on my Thinkpad laptop mounted at /boot/efi:
df -T -H /dev/nvme0n1p1

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/nvme0n1p1 vfat  998M   46M  952M   5% /boot/efi

We can also see human readable information for the file system containing the file /backuplxdimages/ncbz01/01-03-2021/rss-feed-worker-backup.tar.xz:
df -HT /backuplxdimages/ncbz01/01-03-2021/rss-feed-worker-backup.tar.xz

Filesystem             Type    Size    Used   Avail Capacity  Mounted on
zroot/backuplxdimages  zfs     3.5T    350G    3.1T    10%    /backuplxdimage

Show statistics about the number of free and used inodes

Pass the -i as follows:
df -i
df -i -H
df -i -H -T

Now we number of inodes too:

Filesystem     Type     Inodes IUsed IFree IUse% Mounted on
udev           devtmpfs   2.1M   317  2.1M    1% /dev
tmpfs          tmpfs      2.1M   556  2.1M    1% /run
/dev/xvda1     ext4        41M  105k   41M    1% /
tmpfs          tmpfs      2.1M     1  2.1M    1% /dev/shm
tmpfs          tmpfs      2.1M     3  2.1M    1% /run/lock
tmpfs          tmpfs      2.1M    18  2.1M    1% /sys/fs/cgroup
/dev/loop0     squashfs     17    17     0  100% /snap/amazon-ssm-agent/2996
/dev/loop1     squashfs     16    16     0  100% /snap/amazon-ssm-agent/3552
/dev/loop3     squashfs    13k   13k     0  100% /snap/core/10958
/dev/loop4     squashfs    11k   11k     0  100% /snap/core18/1988
/dev/loop5     squashfs    11k   11k     0  100% /snap/core18/1997
/dev/loop6     squashfs   1.6k  1.6k     0  100% /snap/lxd/20371
tmpfs          tmpfs      2.1M     3  2.1M    1% /var/snap/lxd/common/ns
/dev/loop8     squashfs    13k   13k     0  100% /snap/core/11081
/dev/loop2     squashfs   1.6k  1.6k     0  100% /snap/lxd/20400
tmpfs          tmpfs      2.1M    22  2.1M    1% /run/user/1000

Show all mount points, including those that were mounted with various flags

Pass the -a to see all file systems:
df -a
In most cases, you may want to hide output from filesystem such as udev, tmpfs, cgroup, securityfs, lxcfs, devfs and so on. We can do this using the grep command/egrep command or by passing the -t FS_TYPE option to limit listing to file systems of type FS_TYPE::
df -T | grep -v squashfs
df -T | egrep -v 'squashfs|lxcfs|udev'
df -t ext4
df -t zfs
df -t zfs,ext4,vfat

Disk alert shell script

Here is a sample shell script that monitors my AWS EC2 and Linode server disk space usage and sends an email alert and push mobile notification when disk usage goes above 90%

#!/bin/bash
# Name: disk-monitor-script.sh
# Usage: Poor mans disk space monitoring script to alert sysadmin/developer before running
#        out of disk/partition space.
# Author: Vivek Gite under GPLv 2.x+
#
# Warning: *** DO NOT USE THIS SCRIPT BLINDLY. *** 
#          *** This script mostly fails in your environment if requirements are not met. *** 
#
# Prerequisite (or as per your set up): 
#    You must have working SMTPD/email routing in place and subscription to pushover
#    notifications service for Android, iPhone, iPad, etc all:
#    a) Pushover - https://www.cyberciti.biz/mobile-devices/android/how-to-push-send-message-to-ios-and-android-from-linux-cli/
#    b) AWS SES - https://www.cyberciti.biz/faq/how-to-send-emails-with-postfix-and-amazon-aws-ses-on-rhel-centos-8/
# 
# Tested on - RHEL 7.x/8.x and Debian/Ubuntu LTS versions 
# 
# Last revised - 12/Jan/2017 by Vivek Gite, added $DC_P
# -----------------------------------------------------------------------------------------
DC_P="AWS" # either AWS or Linode 
ADMIN="foo@somewhere-tld"
FROM="system-user-ec2@my-corp-tld"
SUBJECT="Alert out of disk space - $HOSTNAME [${DC_P}]"
 
# Set alert level 90% is default
ALERT=90
 
# See https://www.cyberciti.biz/mobile-devices/android/how-to-push-send-message-to-ios-and-android-from-linux-cli/
source /home/scripts/cli_app.sh
 
# Send html email and push notification to my iPhone/Android device
df -H | grep -vE '^Filesystem|tmpfs|udev' | awk '{ print $5 " " $1 }' | while read -r output;
do
  usep=$(echo "$output" | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo "$output" | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
(
echo "From: $FROM"
echo "To: $ADMIN"
echo "Subject: $SUBJECT"
echo "Content-Type: text/html"
echo
 
echo "<html>"
 
echo "<p>Hostname : $HOSTNAME</p>"
echo "<p>Date : $(date)</p>"
 
echo "<p>Running out of disk space on: <strong>$partition</strong> <span style='color: rgb(255, 0, 0);'>${usep}</span>%</p>"
 
echo "<p><h4>Current disk space usage:</h4>"
echo "$(df -H | column -t)"
echo "</p>"
 
echo "<p><h4>Current memory usage:</h4>"
echo "$(free -m)"
echo "</p>"
 
echo "You own me one."
echo "<p>-- $0</p>"
 
echo "</html>"
echo
) | sendmail -f "$FROM" "$ADMIN"
 
  push_to_mobile "$0" "Alert out of disk space - $HOSTNAME [${DC_P}]
 
  Running out of disk space on: $partition (${usep}%)
 
  Current disk space usage:
  $(df -H | column -t)
 
  "
  fi
done

Run this script using cron job:
@daily /path/to/disk-monitor-script.sh

Getting help about df command

All of the above are standard df command options. However, the different variant of Unix offers additional options. You can read about those option by typing the following man command or view BSD/df version online which is used by *BSD and macOS. See GNU/Linux df version help page here:
man df
df --help

Summing up

You learned about the df command and its option on Linux or Unix. We explained various df command examples too. Linux developers and Unix sysadmin need to monitor disk space usage. Otherwise, you risk running out of it. Here is a quick synopsis for your references:

The df command summary for Unix/macOS/*BSD and Linux
df option Description
-P POSIX output format. Explicitly use 512 byte blocks.
-k 1024-byte blocks (default)
-m 1M-byte blocks
-g 1 Gibibyte blocks
-h Human readable (e.g. 1K 243M 2G)
-H Human readable with 1000 blocks
-T Print filesystem type
-t type Limit listing to file systems of type TYPE
-a Show all filesystems
-i Get info about Inodes
-B SIZE Set blocksize. You can also use the BLOCKSIZE environment variables. For example,
BLOCKSIZE=512 df

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum