0

I'm doing a directory-monitoring project. I'd to write a bash script that will calculate the md5checksum of a directory, and store that variable for later comparison. This script will have to run by cronjob and will execute a command if said variable doesn't match the earlier set one.

When I run

echo -n /path/to/directory | md5sum

I'm certain that it is only calculating the md5 of the string "/path/to/directory", and not the directory itself.

Is this possible? If so, how do I then store that variable for later comparison by the next instance of the script?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Darius
  • 612
  • 2
  • 11
  • 23
  • 1
    Your question might be covered by this similar one: "How can I calculate an md5 checksum of a directory?": http://stackoverflow.com/questions/1657232/how-can-i-calculate-an-md5-checksum-of-a-directory – Mike Feb 06 '13 at 14:50
  • duplicated? http://stackoverflow.com/questions/6475252/bash-script-watch-folder-execute-command – Kent Feb 06 '13 at 14:54
  • Have you looked at [incrond](http://inotify.aiken.cz/?section=incron&page=about&lang=en)? – Perleone Feb 06 '13 at 14:58
  • I am unable to install additional packages on this server; I would've loved to use something like inotify-wait or the incrond solution. The other solutions I found did not seem to fit what I am trying to achieve. Would it be best to run this via cronjob or in a never-ending loop? – Darius Feb 06 '13 at 15:08
  • do you want to calc the md5sum for the data in the File Allocation Table for that directory (or OS equivalant (sp!)) OR for the sum-total of all files (their text, date, etc) in that dir structure? – shellter Feb 06 '13 at 15:42
  • I believe the File Allocation Table would be best. – Darius Feb 06 '13 at 19:27

2 Answers2

0

How about ls /path/to/directory | md5sum?

how do I then store that variable for later comparison by the next instance of the script? The easiest solution would be store it in a file.

KBart
  • 1,580
  • 10
  • 18
  • But it only covers a structure of a directory, not the files contest (it wasn't clear which one is that was asked for). *ls -l* could be used instead, so files size, permissions and date modified will be taken into an account. – KBart Feb 06 '13 at 14:53
0

md5sum only handles files so you will have to produce a list of filenames. find with the -type f parameter is a good way to recursively get the contents of a directory.

If you store the output of md5sum in a file then you can use md5sum -c to check which files have changed.

Martin
  • 5,119
  • 3
  • 18
  • 16