hi I want to create a bash file on linux, which checks the md5 hash of a file against a backup md5 hash, so i know if the original file has been tampered with. The script should output the md5 hash of two files and than compare the two created hash files:
md5sum file1 > file1.md5 | cut -c -32
if [ file1.md5 =! backup.md5 ] then;
#send email
but it does not work, there is still the filename in the file.md5, has someone an idea on how to only get the has to the file.md5?
There are several issues with your script.
First you apply cut -c -32
after you have redirected the md5sum output to file1.md5 already, which is why it does nothing.
You should restructure it like that, instead:
md5sum file1 | cut -c -32 > file1.md5
Next, you can not really compare files with =!
directly, you have to read and compare their content instead, like this:
[ "$(cat file1.backup.md5)" != "$(cat file1.real.md5)" ]
Also note that md5sum
does already have a "check mode", so you can simply do this:
#Save MD5 sum of your file to backup.md5, in md5sum native format
md5sum file1 > backup.md5
#Later on ...
if ! md5sum -c backup.md5; then
...