• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

Need shell script for get directory size

Hi,

Need a  full running shell script for get directory size and it send mail automatically  to root user.
0
xeroxzerox
Asked:
xeroxzerox
2 Solutions
 
Vijay Pratap SinghCommented:
#!/bin/bash

# limit on the size (in KB) of the directory
limit=1048576

BASE=/raid/userhome
adminmail=admin@domain.com

while read NAME EMAIL
do      
# if directory exists, find out it's size
if [ -d $BASE/$NAME ]
then
  size=$(/usr/bin/du -sk $BASE/$NAME | cut -f1)
else
  echo "$BASE/$NAME is invalid !!!" | /usr/bin/mail -s "Invalid directory" $adminmail
  exit
fi

if [ -d $BASE/$NAME ]
then
  size=$(/usr/bin/du -sk $BASE/$NAME | cut -f1)
else
  echo "$BASE/$NAME is invalid !!!" | /usr/bin/mail -s "Invalid directory" $adminmail
  exit
fi

# if directory size is greater than limit, alert the user
if [ $size -gt $limit ]
then
  echo "Data volume in your user home dir $BASE/$NAME with a total size of  ${size} kB exceeds the maximum limit of $limit kB (1 GB) ! Please take action!" | /usr/bin/mailx -s "user home dir exceeds max limit" $EMAIL
fi

done < /usr/local/etc/userlist.txt
0
 
xeroxzeroxAuthor Commented:
Please don't copy past from google...

i've already done this but it unable to send mail and determine size of directory....
0
 
Vijay Pratap SinghCommented:
Its same script i am using, Else you can also place a cron job that can send mail to a particular id with output of du -sh or df -h.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
xeroxzeroxAuthor Commented:
ok....so where it's log generate because it not send mail.....
0
 
xeroxzeroxAuthor Commented:
please explain me here that line
 [ -d $BASE/$NAME ]

here $NAME what represent...
0
 
Vijay Pratap SinghCommented:
It means the name of the directory xD
0
 
woolmilkporcCommented:
So you want to get the size of a single directory including subdirectories and alert root when/if a certain threshold value has been exceeded?

That's basically:

dir="/my/dir"
thr="999"  # <- threshold value in MB
[[ -d $dir && $(du -xms $dir |cut -f1) -gt $thr ]] && df -m $dir | mailx -s "Directory $dir threshold $thr MB exceeded" root

The mail body will contain information about the filesystem where the directory is in (df), but we can place other things there, of course.

By the way "-d /directory" is a test for the existence of a directory "/directory".
See "man test" for more.
0
 
rajeev2353Commented:
hi,
create a file => foldersize.sh
#vi foldersize.sh
#!/bin/bash
du -h --max-depth=1 /dir/subdir/ | sort -n -r > /root/dir.xls
(do it as separate folder)
uuencode /root/dir.xls /root/dir.xls |
mutt -s "User Folder Size List With Attachment" -a /root/dir.xls -- mail-id@domain < /root/body.txt < /root/dir.xls

Note- install mutt and sharutils for attachment with mutt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now