Figuring Out Available Disk Space

Related to disk quota management is the simpler question of how much disk space is available on the system. The df command reports disk usage on a per-disk basis, but the output can be a bit baffling:

$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdb2 25695892 1871048 22519564 8% /
/dev/hdb1 101089 6218 89652 7% /boot
none 127744 0 127744 0% /dev/shm

What would be much more useful is a version of df that summarizes the available capacity values in column four and then presents the summary in a way that is easily understood. It’s a task easily accomplished in a script.

#!/bin/sh

# diskspace – Summarizes available disk space and presents it in a logical
# and readable fashion.

tempfile=”/tmp/available.$$”
trap “rm -f $tempfile” EXIT

cat << ‘EOF’ > $tempfile
{ sum += $4 }
END { mb = sum / 1024
gb = mb / 1024
printf “%.0f MB (%.2fGB) of available disk space\n”, mb, gb
}
EOF

df -k | awk -f $tempfile

exit 0

This script can be run as any user and produces a succinct one-line summary of available disk space.

On the same system on which the df output shown earlier was generated, the script reports the following:

$ diskspace
96199 MB (93.94GB) of available disk space

Another issue to consider is whether it’s more useful to know about the available disk space on all devices, including those partitions that cannot grow (like /boot), or whether reporting on user volumes is sufficient. If the latter is the case, you can improve this script by making a call to grep immediately after the df call. Use grep with the desired device names to include only particular devices, or use grep -v followed by the unwanted device names to screen out devices you don’t want included.