Get available size / free space in Bash

By xngo on February 21, 2019

Here is a simple function to get free space in kilobytes of a particular path.

# Get free space of provided path in kilobytes.
function get_free_space_kb()
{
  local location=$1
  local free_space_mb=$(df -k --output=avail ${location} | tail -1 )
  echo ${free_space_mb}
}
 
# How to call it.
get_free_space_kb /tmp

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.