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

sharma ji

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.

Answer this Question

Answers

4 total
Sandra Henry-Stocker
Vote Up (4)

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
Sandra Henry-Stocker

And change the "file changed" message to "$file changed" and your file name will be included in the subject line.

Sandra Henry-Stocker

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!

sharma ji

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..??

Ask a question

Join Now or Sign In to ask a question.
A high-tech Texas gun designer has started shipping its first generation shooting system that combines a hunting rifle with a Linux-based scope that takes so much guesswork out of hitting targets a quarter mile away that even novices can do it.
A freelance Java developer claims it took him only 30 days to build and launch a basic open source office suite that runs on multiple OSes.
With Bitcoin all the rage and startups popping up left and right, it's hard to know who's an expert in the virtual currency and who just has an opinion. Most people would put Jeff Garzik in the former camp.
It's a difficult and time-consuming procedure with no practical purpose, of course, but when has that ever stopped us?
Regular PCWorld readers may recall Cinnarch, a Linux distribution I covered last fall that combined Arch Linux with the relatively new and alternative Cinnamon desktop environment.
Open data initiatives are all the rage among governments around the world, meaning the answers to lots of interesting questions are at your fingertips
A new 'Do Not Track' option and an expanded Social API are among the highlights to expect in this new release.
Canonical on Wednesday announced its next moves on the way to market with a Linux-powered phone.
On the ISS, Linux is in and XP is out and open source is the way of the future.
The Donald wants FundAnything to be like Kickstarter, only gaudier
Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+