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 (8)

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.
OrFoxOS combines Tor with Firefox OS on a $25 smartphone to protect your mobile privacy. Plus: LibreOffice 4.3 released, and Unreal Tournament deathmatch video.
In response to a query from Vint Cerf, professional developers explain why they don’t feel a membership in the Association for Computing Machinery is worth the cost
Can Android be made truly free and open source? Plus: Why open source mobile operating systems have failed, and Minimal Linux Live has been released.
The venerable GNU compiler wins the ACM’s Programming Languages Software Award while simultaneously coming under fire from the Linux creator
Did Microsoft make its biggest mistake by saving Apple? Plus: The best desktop environments for Linux, and the horror game Darkwood comes to Linux.
Canonical has released Ubuntu 14.04.1 LTS, a point release that focuses on bug fixes and security updates. Plus: The NY Times bashes open source for not making enough money, and read a review of Deepin 2014 by LinuxBSDos.
Gaming site GOG.com announced its official support of Linux, with more than 50 games available and many more to follow. Plus: A distrohopper settles down with Debian, and a slideshow of KDE 5.
The U.K. government has adopted ODF as its standard for the exchange of word processor and spreadsheet files between departments and with citizens and suppliers, meaning that companies and citizens will not be required to buy a particular application or software suite in order to collaborate with government staff.
Microsoft is in full blown panic mode about the popularity of Chromebooks. Plus: Download Firefox 31 for your system, and how to install different desktops in Linux.
VolksPC is a new distribution that tries to combine Debian and Android applications on one ARM-based computer. Plus: Avoiding Android vampire apps, and take the Linux.com open source cloud project survey.
Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+