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.find dir1 dir2 -type f -print0 | xargs -0 md5sum >md5sums
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
(most commonly paths to filenames in 'md5sums' are relative or else they're absolute in which case no harm done chdir()ing).chkmd5 md5sums
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 | |
by blazar (Canon) on Jan 30, 2005 at 21:20 UTC | |
by thor (Priest) on Jan 31, 2005 at 02:07 UTC | |
by dimar (Curate) on Feb 01, 2005 at 01:24 UTC |