How to use md5sum for checksum with an md5 file which doesn’t contain the filename

hashsum

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

Best Answer

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
Related Question