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__

Replies are listed 'Best First'.
Re: md5sum checker
by thor (Priest) on Jan 30, 2005 at 16:37 UTC
    I'm not sure I understand what this script accomplishes over 'md5sum -c md5sums', where md5sums is the file generated by your find command above.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      I'm not sure, as I've not tried it yet... in fact I <embarassed> didn't even know about it. Now that I know I wonder wether it would be sensible to remove the whole thing...
        Don't be embarrased...that wasn't my intent. On the plus side, you learned something and you stretched your perl muscles in the process. That's never worth nothing (double negative aside).

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

        Not to mention some readers may benefit from your post (e.g., people stuck on windows machines).