on Technology

Mac OS X: Replicating md5sum Output Format

The md5sum program is used to calculate and verify 128-bit MD5 hashes. This program is installed by default in most Unix, Linux, and Unix-like operating systems including BSD. Mac OS X is a BSD variant and it also includes the md5sum program. However, the program is called md5 instead of md5sum and outputs an MD5 checksum in a different format than the standard md5sum program.

Here's what the standard md5sum output looks like:

Now here's what the output of md5 on Mac OS X looks like:

While this normally wouldn't be a big deal, it can cause major issues if you're trying to run scripts that were written for a Unix-like environment which expect the default md5sum format.

Thankfully, md5 has a switch that reverses the output:

If you'd like to permanently change md5's behavior to mimic that of md5sum, you have two options:

The first is to simply add the following alias to ~/.profile:

Now when you type 'md5sum test.txt', the command will be replaced with 'md5 -r test.txt'. However, this may not work with your scripts.

The second solution, and my preferred method, is to create a small script called md5sum that contains the following:

I then make this script executable (chmod +x md5sum) and put it in /sbin/. Now, whenever a script calls md5sum, the small bash script above is used and it produces output identical to that of md5sum on other Unix systems.

Leave a Reply for Afroz Cancel Reply

Write a Comment

Comment

15 Comments

  1. That won’t work when you have more than one argument or argument(s) that have embedded spaces, such as md5sum /sbin/s* Try the following:

    #!/bin/bash
    /sbin/md5 -r “$@”

  2. Thanks for this … Oddly, the man page for md5 on Mac OS X Snow Leopard does not mention the -r option, so this information you’ve posted is especially helpful.

    • Hey Peter, you’re very welcome!

      Interestingly, I just checked the man page for md5 on my system (also Snow Leopard, 10.6.3) and it does mention the -r option.

  3. I have an issue. On giving the following on the prompt:
    md5sum -c 1.txt

    I get the report as:

    usage: md5sum [-bv] [-c [file]] | [file…]
    Generates or checks MD5 Message Digests
    -c check message digests (default is generate)
    -v verbose, print file names when checking
    -b read files in binary mode
    The input for -c should be the list of message digests and file names
    that is printed on stdout by this program when it generates digests.

    What am I doing wrong. Please note the content of 1.txt is as below:

    2e399d5b1db279126728ffac439cfe2c temp.pdf

    • Hi MKB,

      Does the temp.pdf file exist in the same directory as the 1.txt file? The md5sum program will read 1.txt and then look for temp.pdf in the current directory so that it can validate it.

  4. I noticed that ‘md5sum’ on debianubuntu includes two spaces between the hash and the filename. On my Leopard mac, ‘md5 -r’ has only one space between the columns.

    So look out if your script parses the output of md5 in a way that relies on that second space being present. In that case you should update your script to compensate, or otherwise somehow get the mac to include an additional space.

  5. Just a doubt, I observed that md5 -s gives a different hash than md5 , and file has just .

    I didn’t read the man page too closely though.

Webmentions

  • Lance Quejada November 8, 2012

    Finally got md5sum working on osx http://bit.ly/cgk6mX http://bit.ly/dlwcdN πŸ˜€ #geek #osx

  • Raam Dev November 8, 2012

    @enogrob BTW, you may also be interested in this other post I wrote: Mac OS X: Replicating md5sum Output Format – http://bit.ly/5s3Pv3

  • Mitchel Jhonson November 8, 2012

    Mac OS X: Replicating md5sum Output Format – Solid State Raam: I then make this script executable ( chmod +x md.. http://bit.ly/5fxJZz

  • Further Reading