Linux - Check free space of directory path

By xngo on October 3, 2020

A way to get what is the free space of a specific directory path is to use the df command. df will give you more information than you need it such as the header name. So, you will need to use other tool to extract the information. In the example below, I used df -k --output=avail to get the available space in kilobytes. Unfortunately, it also returns the header name too. So, I used tail -1 to get the last line, which contains the available space value.

#!/bin/bash
# Description: Check disk space
dir_path="/tmp"
 
free_space=$(df -k --output=avail "${dir_path}" | tail -1)
 
if [ "${free_space}" -lt $((6*1024*1024)) ]; then
    echo "It is less than 6GB."
else
    echo "It is more than 6GB."
fi

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.