df
df reports on the amount of disk space free on each mounted filesystem. It traditionally reports in kilobytes, but GNU df can also report in human-readable form, so 1024KB are shown as 1MB, 1024MB are shown as 1GB, and so on. Recent versions of sort are capable of sorting on this type of output, so it will understand that 1GB is greater than 900MB, for example, but at the time of writing this is not in very wide use, so it’s safest to use KB for reporting and sort -k for sorting.
The brief script that follows uses df to determine which filesystem has the most available free space and whether or not that is large enough for a certain image. This can be used when planning an installation or piece of administrative work; if the server has no filesystems at all available for the install, then remedial work will be required before attempting the install. However, even if /var is full, if this script finds enough space in /home, the administrator may choose to do the install into that directory instead.
$ cat freespace.sh #!/bin/bash required=${1:-2131042} preferred=${2:-/var} available='df -k /var | awk '{ print $4 }' | tail -1' if [ "$available" -gt "$required" ]; then echo "Good news. There is sufficient space in ${preferred}:" df -h $preferred else echo "Bad news. There is not enough space in ${preferred}:" df -h $preferred echo echo "Looking in other filesystems..." ...
Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.