s
sharma ji
Apr 04, 2012

tracking changes to particular files and alert via mail if there are any changes. The mail should contain the name of the file, the original content and the changed content

My project has a requirement of tracking changes to particular files and alert via mail if there are any changes. The mail should contain the name of the file, the original content and the changed content. The files are stored on SUSE linux machine and we require a shell/utility to track this changes.



Thanks in advance.

S
Sandra Henry-Stocker
04/04/2012
This is going to require that you maintain copies of those particular files so that you can report on changes that are made. Then you can use diff to detect changes and send email. The script shown here, for example, would compare files with backups (.bak files) and also keep a log of files that have changed. You'd need to modify it to match your requirements, but it might help you get started. It's assuming the files are in a single directory.




#!/bin/bash

recip="overseer@myorg.com"
dir="/usr/data"

for file in `ls $dir`
do
  if [ ! -f $1.bak ]; then
    cp $1 $1.bak
    exit
  fi
  if [ `diff $1 $1.bak` ]; then
    diff tryme tryme.bak > /tmp/diff$$
    cat /tmp/diff$$ | mailx -s "file changed" $recip
    echo "`date`: $file changes noted" >> /var/log/filechanges.log
    ls -l $file >> /var/log/filechanges.log
    rm /tmp/diff$$
  fi
done
S
Sandra Henry-Stocker
04/04/2012
And change the "file changed" message to "$file changed" and your file name will be included in the subject line.
S
Sandra Henry-Stocker
04/04/2012
Eeks! And change the for file line to exclude the .bak files like this:

for file in `ls $dir | grep -v ".bak"`

Otherwise you'll eventually end up with files called *.bak.bak.bak.bak!
s
sharma ji
04/05/2012

Thank you very much... But I have some questions.... (1)if you are giving

for file in `ls $dir`

then what is the use of "$1" because the list of file will be store in variable file...

 

(2)diff tryme tryme.bak > /tmp/diff$$         ........... what is the use of this command..??

 

 

 

and can we use sum,cksum and md5sum for the given problem..??

Answer this