Shell Script to Monitor Disk Space Usage

more shell scripts

It is a very crucial task to monitor disk space usage on any system. If not properly monitored it can end up with poor performance or application crash.

In this tutorial, I will show you a simple shell script that checks the used space on all mounted devices and warns if the used space is more than a threshold.

Shell Script Monitor Disk Space

Below shell script output the percentage disk space used.




result=`df -kh |grep -v "Filesystem" | awk '{ print $5 }' | sed 's/%//g'`

for percent in $result; do

if ((percent > threshold))

partition=`df -kh | head -$i | tail -1| awk '{print $1}'`

echo "$partition at $(hostname -f) is ${percent}% full"


let i=$i+1


Script Result

bobbin@linoxide:/$ df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 52G 4.7G 45G 10% /
tmpfs 1.9G 0 1.9G 0% /lib/init/rw
udev 1.9G 192K 1.9G 1% /dev
tmpfs 1.9G 2.6M 1.9G 1% /dev/shm
/dev/sda6 92G 22G 66G 25% /home
bobbin@linoxide:/$ ./
/dev/sda6 at is 25% full

How this shell script works

#This set threshold value


#Counter, will be used later, set to 2, since first line in df output is description.


#Getting list of percentage of all disks, df -kh show all disk usage, grep -v – without description line, awk ‘{ print $5 }’ – we need only 5th value from line and sed ‘s/%//g’ – to remove % from result.

result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’`

#for every value in result we start loop.

for percent in $result; do

#compare, if current value bigger than threshold, if yes next lines.

if ((percent > threshold))


#taking name of partition, here we use counter. Df list of all partitions, head – take only $i lines from top, tail -1 take only last line, awk ‘{print $1}’ – take only first value in line.

partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’`

#print to console – what partition and how much used in %.

echo “$partition at $(hostname -f) is ${percent}% full”

#end of if loop


#counter increased by 1.

let i=$i+1

#end of for loop.


2 Comments... add one

  1. Hi,

    I got that error during runtime,Please refer below and take care!

    line 11: ((: /: syntax error: operand expected (error token is "/")


  2. I am not familiar with scripting.Please help me if i want output for multiple remote server.
    Where i need to change the script.


Leave a Comment