Tuesday, October 15, 2013

Comparing the Size of the File

www.unixbabuforum.inI want to run the shell script which will compare the size the same file - Rates.txt. 
If the size of the file did not change, I want to send and email. How can I do this in shell script? 

www.unixbabuforum.inIf a file is being appended to, then it grows, so OK. 

If it is a rotated log, then it will suddenly shrink. In fact, I used to have unused hosts where the size did NOT change at rollover, because all it ever logged was "Log file rotated 13-07-2013 00:00:01 at host foobar". 

It is possible for code to change a byte in the middle of a file, and then retouch the time stamp, so neither the size nor time change but the data does. 

So md5sum or cksum is the only way to be sure. But it's not efficient.

www.unixbabuforum.inFile size is not necessarily the best determination factor for file contents not changing.... if that is your end goal. 

Better to use md5sum and compare those results.

www.unixbabuforum.inCheck the 'stat' command. 
# man stat 
stat - display file or file system status 
stat [OPTION] FILE... 
-c --format=FORMAT 
%s Total size, in bytes


# origSize=$(  stat -c %s Rates.txt )
# do something .....
# newSize=$(  stat -c %s Rates.txt )
# if \[\[ $newSize != $origSize \]\]
then
   echo "Rates.txt file size chagned: original size: $origSize; new size: $newSize" | \
      mail -s "Rates.txt file size change detected" myEmail@localhost
fi



0 comments:

Post a Comment

 
Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM