Using fairly standard tools for * nix environments, you could do the following:
Take a snapshot of the folder
find ./test/* -xdev -exec md5sum {} \; | sort > before.txt
This does is to search recursively from the folder ./test
, files and directories, and try with each of the files to generate a hash
with md5sum
, then the output is ordered with sort
and by last we generate the file before.txt
, this file will contain a line for each directory without the hash
(because it is a folder) and other lines per file, this time with the corresponding hash
Then, simply repeat the process again before comparing:
find ./test/* -xdev -exec md5sum {} \; | sort > after.txt
Now yes, compare before.txt
and after.txt
diff -daU 0 before.txt after.txt
If there are no changes we will not get any output, so we could incorporate this in a script as follows:
DIFF=$(diff -daU 0 before.txt after.txt)
if [ "$DIFF" != "" ]
then
echo "Se ha modificado la carpeta"
fi
Retrieve and save the hash
allows us to evaluate what I was saying, files that have modified their content but not their size. What you have to keep in mind is that to generate this data, it is necessary to read the file completely, so the times will increase depending on the number and size of the files. Also this method does not traquea changes of permissions or attributes.
If you prefer a simpler form, you could avoid the use of md5sum
and simply make a stat -c "%y %s %n" ./test/* | sort > before.txt
, with this we retrieve modification date, size and name. The diff
will work the same and will alarm against any change in this data.