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:



and test.txt.md5:

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

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 
$ 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

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.

  • 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 '15 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.