Shell script for df – disk free command examples in Linux

df (disk) command in Linux is used to check the disk usage filesystem. It has more options to work with disk usage check for filesystem. I had written a detailed article about df (disk free) command examples in below article. Please check it to learn more about every option in df command.

df (disk free) command examples in Linux

Here i have developed a shell script to demonstrate all the df (disk free) command options in a single script. You can copy and execute this script to check all the options of df commands with output. It is an addon script to demonstrate the df command examples. If you want to check each and every command and practice then check this link for more details.

Steps to execute this script:

  • Create a file named df_command_examples.sh in any of the path in the server.
  • Copy the below script to the file you create above and save it.
  • Change the file permission to 755 with below command.
$ chmod 755 df_command_examples.sh
  • Execute the script with below command.
$ sh df_command_examples.sh

Shell script for df (disk free) command examples:

Below script will explain every option in df (disk free) command in one place.

#!/bin/bash

#+-------------------------------------------------------------------------------+
#| Name : df_command_examples.sh                                                 |
#| Author: www.linuxtec.co                                                       |
#| Date : 11_OCT_2022                                                            |
#| Purpose: Demonstrate all the df (disk free) command examples in single script |
#+-------------------------------------------------------------------------------+

df_commands=('df -a' 'df --block-size=M' 'df -h' 'df --human-readable' 'df -H' 'df  --si' 'df -i' 'df --inodes     ' 'df -k' 'df -l' 'df --local   ' 'df --no-sync' 'df  --sync' 'df --output' 'df --output=source,fstype,size' 'df -P' 'df --portability' 'df --total ' 'df -T' 'df --type=tmpfs' 'df -x tmpfs' 'df --exclude-type=tmpfs' 'df --version')

for (( i=0; i<${#df_commands[@]}; i++ ))
do
    echo "====================== ${df_commands[$i]} ======================"
    data=`${df_commands[$i]}`
    echo "$data"
    echo "===================================================="
    echo ""
done

Sample output for this script is below.

====================== df --block-size=M ======================
Filesystem          1M-blocks  Used Available Use% Mounted on
devtmpfs                 565M    0M      565M   0% /dev
tmpfs                    594M    0M      594M   0% /dev/shm
tmpfs                    594M    9M      586M   2% /run
tmpfs                    594M    0M      594M   0% /sys/fs/cgroup
/dev/mapper/cl-root    57592M 5639M    51954M  10% /
/dev/mapper/cl-home    28119M  230M    27889M   1% /home
/dev/sda1               1014M  242M      773M  24% /boot
tmpfs                    119M    4M      116M   3% /run/user/0
====================================================

====================== df -h ======================
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             565M     0  565M   0% /dev
tmpfs                594M     0  594M   0% /dev/shm
tmpfs                594M  8.7M  586M   2% /run
tmpfs                594M     0  594M   0% /sys/fs/cgroup
/dev/mapper/cl-root   57G  5.6G   51G  10% /
/dev/mapper/cl-home   28G  230M   28G   1% /home
/dev/sda1           1014M  242M  773M  24% /boot
tmpfs                119M  3.5M  116M   3% /run/user/0
====================================================

====================== df --human-readable ======================
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             565M     0  565M   0% /dev
tmpfs                594M     0  594M   0% /dev/shm
tmpfs                594M  8.7M  586M   2% /run
tmpfs                594M     0  594M   0% /sys/fs/cgroup
/dev/mapper/cl-root   57G  5.6G   51G  10% /
/dev/mapper/cl-home   28G  230M   28G   1% /home
/dev/sda1           1014M  242M  773M  24% /boot
tmpfs                119M  3.5M  116M   3% /run/user/0
====================================================

Leave a Comment