Disk usage/free space script

I like to monitor disk usage from the command line using the df utility. However, the output can get overwhelming sometimes depending on the system. This is a tiny bash script I use as a df wrapper that does filtering, sorting and uses colors. It takes an argument nc to disable colors.
#!/bin/bash
__diskfree () {
local COLOR_HI
local COLOR_MD
local COLOR_LO
local COLOR_NC
DF="df -h --output=source,target,fstype,pcent,size,avail -x tmpfs -x devtmpfs -x squashfs -x overlay"
SORT="sort -k 4,4 --numeric-sort --reverse"
if [[ ${1} != "nc" ]]; then
COLOR_HI="\e[0;31m"
COLOR_MD="\e[0;33m"
COLOR_LO="\e[0;32m"
COLOR_NC='\e[0m'
fi
echo -ne "$COLOR_HI"
$DF | grep -E "100%|9[0-9]%" | $SORT
echo -ne "$COLOR_MD"
$DF | grep -E "[78][0-9]%" | $SORT
echo -ne "$COLOR_LO"
$DF | grep -E "[^1][ 0123456][0-9]%" | $SORT
echo -ne "$COLOR_NC"
}
__diskfree $1
0 comments
Reply