LinuxQuestions.org
Visit the LQ Articles and Editorials 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
 
Thread Tools
Old 05-23-2006, 04:47 PM   #1
nazs
Member
 
Registered: Apr 2005
Posts: 57
Thanked: 0
Script to compare file size


[Log in to get rid of this advertisement]
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
nazs is offline     Reply With Quote
Old 05-23-2006, 07:55 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 2,300
Thanked: 89
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
jlinkels is offline     Reply With Quote
Old 05-24-2006, 08:21 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Thanked: 0
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 '-'
kshkid is offline     Reply With Quote
Old 05-24-2006, 08:57 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 2,300
Thanked: 89
Yup.

Thanks.

jlinkels
jlinkels is offline     Reply With Quote
Old 05-24-2006, 10:35 AM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: pa, usa
Distribution: redhat-9/ fc-9-live-usb/ aix
Posts: 1,127
Thanked: 35
wc -c counts the number of chars (bytes) in a file.
schneidz is offline     Reply With Quote
Old 05-24-2006, 11:09 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Thanked: 0
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
kshkid is offline     Reply With Quote
Old 05-24-2006, 11:10 AM   #7
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Thanked: 0
in short as,

echo $((`wc -c < filename` - 1))
kshkid is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 11 12-18-2007 04:25 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 01:45 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration