md5sum validation for a file

3.6k views Asked by At

I have two files hello.txt and hello.txt.md5

MD5 output is in the below format

cat hello.txt.md5 
b1946ac92492d2347c6235b4d2611184

I wrote a simple script to validate md5sum.

csum=$(cat hello.txt.md5)
echo "$csum hello.txt" | md5sum -c

This script works fine with Ubuntu 13.10 but it throws below error in Ubuntu 12.04.

md5sum: standard input: no properly formatted MD5 checksum lines found

Can anyone show me how to do this in Ubuntu12.04.

2

There are 2 answers

1
Olli Mk On

At least on my md5sum files generated have two spaces between checksum and file name. Tested your script and it didn't work, added space between $csum and hello.txt and it did.

0
ivan_pozdeev On

The cause is likely to be some change between the md5sum versions.

Doing md5sum <file1> <file2> with both versions should give you an example of what format each version expects.

Specifically, the md5sum man page says:

The default mode is to print a line with checksum, a character indicating type ('*' for binary, ' ' for text), and name for each FILE.

So, for checking in text mode, two spaces shall normally be present (indeed, checking with md5sum -t t.txt confirms this). I guess the new version has lifted this requirement.

Indeed, here's a GNU coreutils commit titled "md5sum: handle BSD reversed format checksums", dated 2011-09-16. It makes md5sum guess between the two formats.
The commit was released in coreutils 8.14 while Ubuntu 12.04 uses 8.13.