Posted By

noah on 07/05/09


file Shell Bash mac compare perl cksum one-liners checksum pbpaste

Versions (?)

comparing the checksums of two files with Perl and cksum

 / Published in: Perl


This one-liner helps to determine if two or more files have the same checksum. It works by piping the output from cksum to Perl, which takes note of the first checksum and compares each subsequent file's checksum to that value.

Assume an example session where we have three identical files and two that are different

>echo bart > bart
>cp bart bart1
>cp bart bart2
>echo milhouse > mvh
>echo lisa > lisa

two files with the same checksum, produce no output

>cksum bart bart1 | perl -ane '$x ||= $F[0]; warn if $x != $F[0];'

if a there is a different checksum, the line numbers printed are the indexes of those files

>cksum bart bart1 mvh bart2 lisa | perl -ane '$x ||= $F[0]; warn if $x != $F[0];'
Warning: something's wrong at -e line 1, <> line 3.
Warning: something's wrong at -e line 1, <> line 5.
  1. cksum file1 file2 | perl -ane '$x ||= $F[0]; warn if $x != $F[0]'
  3. # This can be shortened slightly, to:
  5. cksum file1 file2 | perl -ane 'warn if $F[0] != ($x ||= $F[0])'
  7. # If the checksum value we need to match is already known, that can be
  8. # shortened to:
  10. cksum file1 file2 | perl -ane 'warn if "664253628" != $F[0]'
  12. # on Mac, if you have copied a checksum value into the clipboard, use
  13. # pbpaste to do the comparison
  15. cksum file1 file2 | perl -ane 'warn if `pbpaste` != $F[0]'

Report this snippet  

You need to login to post a comment.