LinuxQuestions.org
Visit Jeremy's Blog.
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, 03: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, 06:55 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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, 07: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, 07:57 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
Yup.

Thanks.

jlinkels
 
Old 05-24-2006, 09:35 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
wc -c counts the number of chars (bytes) in a file.
 
Old 05-24-2006, 10: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, 10: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 07:45 AM
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
Help with file size script jpc82 Programming 10 04-01-2004 04:55 AM
I'm looking for a perlscript to compare size of 2 files cccc Programming 5 02-28-2004 04:45 PM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:43 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
Open Source Consulting | Domain Registration