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.

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

About the author

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