The Student Room Group

Shell Script, Linux, Monitor file changes

I nicely have to do the retake assignment coz my ****** uni lost my assignment.

However also nicely we have to do something never taught at all, and i can't find any help on google.

We need to create a shell script to monitor important changes to files/directories that makes a log of the changes.

any pointers would be amazing
Reply 1
bluehell555
I nicely have to do the retake assignment coz my ****** uni lost my assignment.

However also nicely we have to do something never taught at all, and i can't find any help on google.

We need to create a shell script to monitor important changes to files/directories that makes a log of the changes.

any pointers would be amazing

A first step would be to keep track of file modification dates and/or filesizes, so that you can tell whether a file has changed or not. (I think it would be ok to assume that if a file's modification date hasn't changed and neither has it's size, then it's probably still the same file.)

If you do detect that there's been a change, there's a unix command called [pre]diff[/pre] which spits out a line-by-line comparison of any changes between the two files you give as arguments. Read its man page, or have a look on wikipedia for more.

I think that should probably get you started :smile:
Reply 2
bluehell555
I nicely have to do the retake assignment coz my ****** uni lost my assignment.

However also nicely we have to do something never taught at all, and i can't find any help on google.

We need to create a shell script to monitor important changes to files/directories that makes a log of the changes.

any pointers would be amazing


Have you considered using cron to schedule a job in conjunction with the advice above?
Reply 3
Chrosson
Have you considered using cron to schedule a job in conjunction with the advice above?


You again..

I was just about to mention cron jobs myself. If you don't want to go too advanced into it, then why not only compare the files checksums to a previous one (from the last time the cron job was run) to the one it is now.

Although if you have to monitor directories themselves and changes to new ones/or files and such, then that makes it a whole lot harder..

Also, have a harder look online. I just found this by doing a quick search, theres bound to be more examples with sample code.
Reply 4
Timestamps aren't really trustworthy.

1. they can be fiddled eg touch -d
2. the output from utilities eg ls -l has been adjusted for timezone - which can cause a few problems if you do a simple comparison of timestamps between runs.

checksums are a bulletproof test of changes to a file contents imo and cron is also a good suggestion

depends a lot on what you want to check for ownership, permissions, has it been replaced with a softlink?

if it's a bunch of files maybe you could tar the directory to a pipe and checksum that... then compare the checksum to a previous value - that'd pick up most of those types of change, though not softlinks off the top of my head.

Latest

Trending

Trending