4

I have 2 files test.txt and test.txt.md5. I would like to verify the checksum of test.txt.

The gnu tool md5sum requires an md5 file with the following format "[md5-hash][space][space][filename]" (md5sum -c test.txt.md5). Unfortunately my test.txt.md5 only contains the md5 hash (without the spaces and filename).

How can I pass the hash from the "test.txt.md5" file to the "md5sum -c" command? I guess I have to use the standard input however all examples I have seen try to recreate the md5sum file format

The content of the files is:

test.txt:

test

and test.txt.md5:

d8e8fca2dc0f896fd7cb4cb0031ba249
1
  • cmp test.txt.md5 <(md5sum test.txt | awk '{print $1}') && echo $? If the result returned is 0 then there is a match Apr 29, 2015 at 13:38

2 Answers 2

4

Like many commands, md5sum has the ability to read from the standard input if an option's value is - (from man md5sum):

Print or check MD5 (128-bit) checksums. With no FILE, or when FILE is -, read standard input.

Since you know the file name, you could simply print the contents of your md5 file, a couple of spaces and then the name and pass that to md5sum:

$ cat test.txt.md5 
5a6d311c0d8f6d1dd03c1c129061d3b1
$ md5sum -c <(printf "%s  test.txt\n" $(cat test.txt.md5)) 
test.txt: OK

Another option would be to add the file name to your file:

$ sed -i  's/$/  test.txt/' test.txt.md5
$ md5sum -c test.txt.md5 
test.txt: OK
2

You can re-calculate the MD5 checksum of your file and compare it to that stored in test.txt.md5:

cmp test.txt.md5 <(md5sum test.txt | cut -d\  -f1)

If there's no output then the checksums match.

1
  • I got a error when I ran you suggested command (Missing name for redirect). However a minor change made it work (md5sum test.txt | cut -d' ' -f1 | cmp test.txt.md5 -). Thanks for the cmp hint though! :)
    – Kostas
    Apr 29, 2015 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .