The purpose of this script is to check lists of files' md5sums as generated by (commands like)
find dir1 dir2 -type f -print0 | xargs -0 md5sum >md5sums
Such commands are simple enough not to require a dedicated (perl) script for the generation of lists (also I'm a keen user of hist cmds) whereas I felt the need to write one to check them, even if it may have been doable with shell scripting too, albeit considerably more awkwardly, IMHO. This has proven useful for creating cdroms because I noticed that I randomly get otherwise unsignaled writing errors.

This is actually a complete application for me, but it is short enough that it's indeed better to post it in the snippets section. OTOH it's long enough that someone may legitimately argue about the legitimity of the -l switch... well, to be fair up until a few minutes ago the directory handling code was not there for I simply used to manually chdir where needed and happily run it as

chkmd5 md5sums
(most commonly paths to filenames in 'md5sums' are relative or else they're absolute in which case no harm done chdir()ing).

Briefly, I often thought to add that dir handling code, but I've always been to lazy to do it. Now I just wanted it to be slightly more complete for posting, in which case I'd say that for once my Hubris overtook my Lazyness...

I have also been thinking wether it wouldn't be better to add different exit codes according to wether any problem is encountered or not, and/or cmd line switches to print out relevant information, but basically I routinely just run it on the files I need to: if nothing is printed then everything is ok, otherwise something is not, period.

Update: it seems I really addicted to bugs lately, so I changed the original code to remove an obvious one. Since it is only a matter of moving a line and no one had noticed it, I'm not marking the change in any particular way.

#!/usr/bin/perl -ln use strict; use warnings; use Digest::MD5; use Cwd; use File::Basename; BEGIN { our $base=cwd } if ($. == 1) { chdir $_ or warn "Can't chdir to `$_': $!" for dirname $ARGV; } s/(\w{32})\s+// or die "input data may not be in the correct format"; my $orig=$1; warn("[$ARGV] `$_' doesn't exist!\n"), next unless -e; open my $fh, '<:raw', $_ or warn("Can't open `$_': $!\n"), next; my $new=Digest::MD5->new->addfile($fh)->hexdigest; warn "[$ARGV] md5sum mismatch for `$_'\n" unless $orig eq $new; close ARGV and chdir our $base if eof; __END__

In reply to md5sum checker by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.