We help IT Professionals succeed at work.

Script for calculating disk usage

wfaleiro
wfaleiro asked
on
Medium Priority
846 Views
Last Modified: 2013-12-16
Hi,
I have home folders under /export/home/*.
I need to have  script that will check for disk usage of each folder, user1. Test to see if its over 2GB and if it is then mail the owner of the folder say tom@mydomain.com. This will have to be done recursively for all users in the directory /export/home.

Thanks,
--Walter
Comment
Watch Question

if not use something like this, as for the time beeing i am on windows client you will have to alter alot

:-)
function sendmail has to be added by you,try 'man mail' for hints. but still think using quota is better

#!/bin/sh
#loop dirs
for i in `ls -al /export/home` do
  #check size
 if [ du -sh $i -gt 2000000000 ]; then
  echo "$i is full"
  sendmail($i)
  fi
done

Author

Commented:
Hi,
I cannot use quotas as it will be implemented on an entire parition. The home directory is a sub directory on the partition, with other directories like source for cvs present. These will also come into picture when quotas are implemented.

Regards,
--Walter
du -h  /export/home/user1 | awk -F/ 'NF==3{print}'
du -h  /export/home/ | awk -F/ 'NF==3{print}'

Author

Commented:
That produces no output at all.
please post some lines from (10 or so, at least containing one with just a directory path)
  du -h /export/home/user1
freelance for hire
Commented:
Hi Tom,

Here is a script that will do what you want, "fix" the SUBJECT and the BODY. You might wanna add the "@mydomain.com" after $i in "mail $i..."

//jonas

#!/bin/bash

# limit
LIMIT=42000 #2GB
SUBJECT="Your homedir is to large"
BODY="You need to delete stuff..."
cd /export/home/
for i in *
do
TEST=`du -sk $i|awk '{print $1}'`
if [ $TEST -gt $LIMIT ]; then
mail $i -s "$SUBJECT" <<EOF
$BODY
EOF

fi
done

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.