r/ScriptSwap • • Mar 14 '17

[Bash] Disk space monitor

i have two operating systems on a 128 GB SSD, so i use this as a precaution. i have added it as a regular startup program.

every 10 minutes it checks what percentage of disk space the root partition is taking, if it reaches the warning level there will be a notification pop-up.

#!/bin/bash

# Set percentage
warning_level=80

while true; do
  # Percentage of disk space used
  used_space=$(df -HP / \
    | tail -1 \
    | tr -s ' ' \
    | cut -d' ' -f5 | cut -d'%' -f1)

  if [ $used_space -ge $warning_level ]
  then
    notify-send "System drive is ${used_space}% full               " \
      -i dialog-warning -t 36000000 -u critical
    exit
  fi

  sleep 10m
done
14 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 14 '17

i'm on Linux Mint and other things are not showing. i have other partitions and mounted usb disk things and they dont show with:

df -HP /

1

u/EasyStevey Mar 14 '17

You are correct - I missed your trailing "/". Either way you can still just use awk function as is and it will work without having to do a tail or any cuts.

bash-3.2# df -HP | awk '/\// { print substr($5, 1, length($5)-1); exit }'

89

bash-3.2#