41 Figuring Out Available Disk Space


#41 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.

The Code

 #!/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 +=  } 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 

Running the Script

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

The Results

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 

Hacking the Script

If your system has lots of disk space across many multigigabyte drives , you might even expand this script to automatically return values in terabytes as needed. If you're just out of space, it'll doubtless be discouraging to see 0.03GB of available disk space, but that's a good incentive to use diskhogs (Script #40) and clean things up, right?

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.




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net