Shell script to watch the disk space

df displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown. Read man page of df if you are new to df command.

Steps

=> Find disk space using df

=> Filter out filesystem and find out the percentage of space using grep

=> Write a shell script

Step # 1: First get disk space:

$ df -H

Output:

Filesystem             Size   Used  Avail Use% Mounted on
/dev/hdb1               20G    14G   5.5G  71% /
tmpfs                  394M   4.1k   394M   1% /dev/shm
/dev/hdb5               29G    27G   654M  98% /nas/www

Step # 2: Next filter out filesystem and find out the percentage of space

$ df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }'

Output:

71% /dev/hdb1
98% /dev/hdb5

Step # 3: Write a shell script

Above command displays field 5 and 1 of df command. Now all you need to do is write a script to see if the percentage of space is >= 90% (download script):

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 90 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep%" you@somewhere.com
  fi
done

Setup Cron job

Save and install script as cronjob. Copy script to /etc/cron.daily/ (script downolad link)
# cp diskAlert /etc/cron.daily/
# chmod +x /etc/cron.daily/diskAlert

OR install as cronjob:
crontab -e

Write cronjob as per your requirement
10 0 * * * /path/to/diskAlert

Updated script version

Tony contributed and updated my script - You can exclude selected filesystem in case you don't want monitor all filesystems.

#!/bin/sh
# set -x
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%.
# -------------------------------------------------------------------------
# Set admin email so that you can get email.
ADMIN="root"
# set alert level 90% is default
ALERT=90
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions.
# An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"
EXCLUDE_LIST="/auto/ripper"
#
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
function main_prog() {
while read output;
do
#echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
  partition=$(echo $output | awk '{print $2}')
  if [ $usep -ge $ALERT ] ; then
     echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" | \
     mail -s "Alert: Almost out of disk space $usep%" $ADMIN
  fi
done
}

if [ "$EXCLUDE_LIST" != "" ] ; then
  df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog
else
  df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog
fi
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 16 comments… read them below or add one }

1 Per Lindahl 03.29.07 at 3:16 pm

I noticed that on devices that have very long path in some cases there might be a line break appended to the output and that will in turn create an error when the script is run like:

line 22: [: -ge: unary operator expected

To remove the line break just append “-P” to the “df” command.

example:
df -HlP

(P) = adds posix compliance to the output.

Errors that occured: df -H
/dev/mapper/volume00-pgvol
50G 31G 19G 62% /localhome/postgresql

with -P
/dev/mapper/volume00-pgvol 50G 31G 19G 62% /localhome/postgresql

Also if you only have one type of filesystem that you need to monitor then add “-t ext3″ will only show EXT3 filesystems.

Hope it helps.
Regards,
Per L

2 nixcraft 03.30.07 at 5:27 pm

Per Lindahl,

Thanks for sharing your tip!

3 Trula 05.06.07 at 11:18 pm

Please show me if I need to get disk file last week or 10 days ago, what do I need to do?
Thank you ~ Trula

4 Mehmet Buyukozer 05.19.07 at 11:55 pm

For my case:

df -h | grep d30 | awk ‘{ print $5 ” ” $1 }’ | while read output;

worked better. Thanks for shell script, life saver one.

5 sorabh harit 07.10.07 at 5:48 am

HI

thanks for script but i am getting following error
on my solaris 9 servrer after executing the scipt.

syntax error at line 5: `usep=$’ unexpected

Regards

Sorabh harit

6 Niek 08.17.07 at 4:08 pm

@sorabh harit:
That is because you run a “real” Bourne shell, which doesn’t understand the syntax on that line.

Also, there is a fair bit of double code at the end, and use of a function isn’t really necessary.

I’ve put up a modified version on my site:
http://www.qwertyboy.org/files/diskspace.txt

Niek.

7 Rohan 09.13.07 at 12:27 pm

Can you please tell me how to find disk usage for a particular user only…!

8 Derek Murphy 11.14.07 at 7:52 pm

I tried using this script but I’m getting a syntax error, trying to run this on linux(redhat as 4) any thoughts?

-bash-3.00$ ./diskscript.sh
13% /dev/cciss/c0d0p2
17% /dev/cciss/c0d0p1
1% /dev/cciss/c0d0p5
./diskscript.sh: line 4: syntax error near unexpected token `do’
./diskscript.sh: line 4: `do’

#!/bin/sh
#df -H | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $1 }’ | while read output;
df -F ext3 -k | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{print $5 ” ” $1}’
do
echo $output
usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
partition=$(echo $output | awk ‘{ print $2 }’ )
if [ $usep -ge 90 ]; then
echo “Running out of space \”$partition ($usep%)\” on $(hostname) as on $(date)” |
mail -s “Alert: Almost out of disk space $usep%” derek@iona.com
fi
done

9 Hari 12.02.07 at 7:10 pm

while executing the below script i am getting the following:

Script:

#!/bin/sh
df -H | grep -vE ‘^Filesystem’ | awk ‘{ print $5 ” ” $1 }’ | while read output;
do
echo $output
usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
partition=$(echo $output | awk ‘{ print $2 }’ )
if [ $usep -ge 50 ]; then
echo “Running out of space \”$partition ($usep%)\” on $(hostname) as on $(date)” |
mail -s “Alert: Almost out of disk space $usep%” Hari-Krishna.Reddy@in.standardchartered.com
fi
done

Error:

sh diskAlert.sh
/dev/mapper/rootvg-rootvol
diskAlert.sh: line 7: [: /dev/mapper/rootvg-rootvol: integer expression expected
/ 4.2G
diskAlert.sh: line 7: [: /: integer expression expected
14% /dev/sda1
0% none
/dev/mapper/rootvg-homevol
diskAlert.sh: line 7: [: /dev/mapper/rootvg-homevol: integer expression expected
/home 2.1G
diskAlert.sh: line 7: [: /home: integer expression expected
/dev/mapper/rootvg-sharedvol
diskAlert.sh: line 7: [: /dev/mapper/rootvg-sharedvol: integer expression expected
/shared 6.3G
diskAlert.sh: line 7: [: /shared: integer expression expected
/dev/mapper/rootvg-varvol
diskAlert.sh: line 7: [: /dev/mapper/rootvg-varvol: integer expression expected
/var 2.1G
diskAlert.sh: line 7: [: /var: integer expression expected

can any one help me on this.

10 Ambika 01.25.08 at 11:16 am

i want the disk space scripts where it should send the mail in the .lst file

11 david 07.22.08 at 5:08 am

i am able to run the shell script properly but if i put it in a crontab its not working fine
can anyone plz suggest me in this

thnks
david

12 jack 09.01.08 at 10:25 am

Can you send your crontab scripts?

13 Prasanna 10.08.08 at 8:31 am

Hi

I have to select 10% of the files from a 1 million records file at random. Could anyone help me with a shell script?
thanks and regards
Prasanna

14 adgar marks 01.21.09 at 4:08 pm

I am using Gentoo and i get the following error:

./freespace.sh: line 25: [: Ben: integer expression expected

Once i do “echo $output”, i get an empty line and i dont know what wasi suppose to expect from $output.

I hope someone could help me a bit

thansk

15 jack 05.05.09 at 1:17 pm

hello..

what if your both partitions will run out of space? you will receive 2 mails. if you have more partitions, then you will receive more mails… how you do to receive all messages in one mail?

16 Neo 06.12.09 at 11:43 am

Hi,

I have typed in df -h, all my files were display. I have found that my log file is full, file name /lsx/log sitting at 100%, then I typed in the command du -akd |sort -rn | more to sort from big to small, I found that /fb was the biggest and tried to clean up with then command cat /dev/null > fb, but didn’t clean up any of the files. Please advice me how to clean up log files when full.

Regards,
Neo

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , ,

Previous post: Repairing ReiserFS file system with reiserfsck

Next post: Linux: Finally, full read and write support for NTFS available for download