dirty-helpers/free-gigs

30 lines
634 B
Bash
Executable file

#!/bin/sh
# script-friendly df
factor=1024 # for the default (GiB); use -H for GB
optstring="H"
while getopts "$optstring" opt; do
case $opt in
h) factor=1024 ;; # default (GiB)
H) factor=1000 ;; # decimal (GB)
esac
done
shift $((OPTIND-1))
target=${1:-.}
if free_bytes=$(stat -f --format="%a*%S" "$target" 2>/dev/null); then
# GNU stat (Linux)
:
elif free_bytes=$(stat -f "%a*%S" "$target" 2>/dev/null); then
# BSD/macOS stat
# XXX untested
:
else
# BusyBox stat fallback
free_bytes=$(stat -f -c "%a*%S" "$target")
fi
free_gb=$((free_bytes / factor / factor / factor))
echo "$free_gb"