Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

md5sum output only hash in file

Tags:

bash

md5

md5sum

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?

like image 498
MadMax Avatar asked Sep 16 '25 13:09

MadMax


2 Answers

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
...
like image 125
zeppelin Avatar answered Sep 18 '25 10:09

zeppelin


You have the wrong order of the commands. Write the

md5sum file1 | cut -c -32 > file1.md5
like image 43
TMS Avatar answered Sep 18 '25 09:09

TMS