LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-20-2009, 05:14 PM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
bash script - calculate directory size


I'm trying to write a script that will calculate a directory size and if the size is greater than 4GB, it will send out an email. But I am getting the "integer operation expected" error when it tries to calculate the current disk size. Here's my script. Thanks for any help:

Code:
TO="user@email.com"

SUBJ="Ready for DVD burning on `hostname`"

MAIL="/bin/mail"

# 4GB
SIZE="4294967296"

# check the current size
CHECK="`du /data/sflow_log/`"

if [ "$CHECK" -gt "$SIZE" ]; then
        "$MAIL" -s "$SUBJ" "$TO" < notice_email
else
        exit 1
fi
 
Old 01-20-2009, 05:25 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,489

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
The output of du contains two items: the size of the directory and the directory name. Hence you have to parse the output stripping out the directory name, otherwise the expression -gt (integer comparison) fails. For example:
Code:
CHECK=$(du -bs /data/sflow_log | awk '{print $1}')
the -b option gives the size in bytes (otherwise default is kbytes) and the -s option prevents du to give the size of each subdirectory inside /data/sflow_log, resulting in a multi-line output. Finally the awk command extract the first field of the output line, that is the size only. Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH : Total directory size xgreen Programming 7 03-04-2013 05:33 AM
calculate logarithm in bash script bluesmoke Programming 5 12-14-2008 01:41 AM
bash - size of home directory klavuzkarga Linux - Newbie 9 12-17-2007 05:58 AM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 11:35 AM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 09:26 PM


All times are GMT -5. The time now is 12:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration