LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 05-23-2006, 04:47 PM   #1
nazs
Member
 
Registered: Apr 2005
Posts: 57

Rep: Reputation: 15
Script to compare file size


Hi All,
I am looking for a script to compare file size and if the newest file is less than or equal to the older file i would like it to send me an email. Basically i had database backups getting scp'd to another box and when i needed to restore a database i found out that the file was not being fully transfered. I keep 3 days worth of backups of each db. But i want to just compare the 2 most current. So lets say i have nazs.sql.gz . It gets newcopied and becomes nazs.sql.gz,1 and then the new file comes in and overwrites nazs.sql.gz. Now i want to compare the file size of nazs.sql.gz and nazs.sql.gz,1. If nazs.sql.gz is less than or equal to nazs.sql.gz,1 then i want to send an email. There are around 20 databases being backed up. I would need to know how to put the list of databases into the script so it would check them all. I hope i explained this well enough. If there are questions please let me know.

Thanks,
Nazs
 
Old 05-23-2006, 07:55 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,243

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
If you don't mind, I'll give you some general pointers and code snippets from my own. You should be able to tweak them to your own needs, these is by no means a ready to run program!

If you want to do something with a number of files:

Code:
#turn off globbing
set -f

FCL=" -iname *.pdf -print -o -iname *.html -print "
dir="/tmp"


for file in $( find $dir -type f $FCL )
do
#        compare size etc.
done
To compare the size of a file:

Code:
fsize_first=` ls- l fname | awk '{print $5}'`
fsize_now=` ls- l fname.1 | awk '{print $5}'`
if [ $fsize_first -eq $fsize_now ]
then
   # file sizes are equal, do something
fi
To send myself an e-mail, I prefer sendEmail (or sendemail, not sure what the case is of the program name). Very simple and straightforward.

You also might want to peek in the advance bash scripting guide if you haven't done also.

jlinkels
 
Old 05-24-2006, 08:21 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
fsize_first=` ls- l fname | awk '{print $5}'`
fsize_now=` ls- l fname.1 | awk '{print $5}'`
i believe its a typo,

it should be

fsize_first=`ls -l fname | awk '{print $5}'`
fsize_now=`ls -l fname.1 | awk '{print $5}'`


note the space between 'ls' and '-'
 
Old 05-24-2006, 08:57 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,243

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Yup.

Thanks.

jlinkels
 
Old 05-24-2006, 10:35 AM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,213

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
wc -c counts the number of chars (bytes) in a file.
 
Old 05-24-2006, 11:09 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by schneidz
wc -c counts the number of chars (bytes) in a file.
wc -c includes the line terminator

for the actual byte count
you need to decrement by 1
 
Old 05-24-2006, 11:10 AM   #7
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
in short as,

echo $((`wc -c < filename` - 1))
 
  


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
File size using BASH script cb951303 Programming 16 11-07-2012 08:45 AM
File Size Shell Script BlackLabel Programming 7 11-27-2007 08:48 PM
Help with file size script jpc82 Programming 10 04-01-2004 05:55 AM
I'm looking for a perlscript to compare size of 2 files cccc Programming 5 02-28-2004 05:45 PM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 09:30 AM


All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
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