Solved

Need shell script for get directory size

Posted on 2013-06-23
8
487 Views
Last Modified: 2013-06-25
Hi,

Need a  full running shell script for get directory size and it send mail automatically  to root user.
0
Comment
Question by:xeroxzerox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:Vijay Pratap Singh
ID: 39270361
#!/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
 
LVL 6

Author Comment

by:xeroxzerox
ID: 39270364
Please don't copy past from google...

i've already done this but it unable to send mail and determine size of directory....
0
 
LVL 6

Expert Comment

by:Vijay Pratap Singh
ID: 39270365
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 6

Author Comment

by:xeroxzerox
ID: 39270369
ok....so where it's log generate because it not send mail.....
0
 
LVL 6

Author Comment

by:xeroxzerox
ID: 39270373
please explain me here that line
 [ -d $BASE/$NAME ]

here $NAME what represent...
0
 
LVL 6

Expert Comment

by:Vijay Pratap Singh
ID: 39270396
It means the name of the directory xD
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 100 total points
ID: 39270609
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
 
LVL 3

Accepted Solution

by:
rajeev2353 earned 400 total points
ID: 39274316
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
centos linux 65 192
android secure ftp 3 73
how to configure linux OS using Ubuntu 7 69
How to use IFS to get output of range in comma separator in bash? 2 47
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question