LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Script to compare file size (http://www.linuxquestions.org/questions/programming-9/script-to-compare-file-size-447828/)

nazs 05-23-2006 03:47 PM

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

jlinkels 05-23-2006 06:55 PM

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

kshkid 05-24-2006 07:21 AM

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 '-'

jlinkels 05-24-2006 07:57 AM

Yup.

Thanks.

jlinkels

schneidz 05-24-2006 09:35 AM

wc -c counts the number of chars (bytes) in a file.

kshkid 05-24-2006 10:09 AM

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 05-24-2006 10:10 AM

in short as,

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


All times are GMT -5. The time now is 07:06 PM.