Calculate CRC32 checksums with cksum

Dec 28, '04 09:28:00AM

Contributed by: guybrush

Mac OS X comes with a nice commandline tool called cksum to calculate CRC16/32 checksums of files. The only downside is that the output of the CRC is in decimal only. So I made a small one-liner to convert the output to hex to make it more usable in the Internet world.

The oneliner is written for the Bourne shell (bash), and uses cksum in combination with ruby. This should be one long line; replace the line break with a single space when you enter the function:

function crc32 { cksum -o3 "$@"|ruby -e 'STDIN.each{|a|a=a.split;printf 
 "%08X\t%s\n",a[0],a[2..-1].join(" ")}'; }
Here's an example of the new function in use:
$ cd "/Library/Desktop Pictures/"
$ crc32 Aqua\ Blue.jpg 
FEDCA2D9        Aqua Blue.jpg
$ crc32 *.png
59B02128        Small Ripples graphite.png
FA28E0A8        Small Ripples.png
You can also put the function in your .bash_profile so you can use the crc32 command in all your Terminals. Of course there are existing CRC32/SFV tools and this is not to replace them, but this is a nice alternative making use of the standard tools that come with the OS.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041227082911874