Hi,
this script takes an existing MD5 checksum file and a list of files to add to this checksum file if there are no MD5 sums for them yet.
I'm using it to update existing MD5 files when I got new data files.
Usage example:
addmd5 md5sums *.avi
use strict;
use warnings;
use Digest::MD5;
my $md5file = shift;
my @files = @ARGV;
die "Usage: addmd5 <md5sumfile> <files>\n" if !defined $md5file or ! -
+e $md5file or !@files;
# Read existing MD5 sums
open (MD5FILE, '<', $md5file) or die "Couldn't open '$md5file'.\n";
my %md5files = map { chomp; substr($_, 34) => 1 } <MD5FILE>;
close (MD5FILE);
# Append new MD5 sums
open (MD5FILE, '>>', $md5file) or die "Couldn't open '$md5file' for ap
+pending.\n";
foreach my $file (@files) {
next if exists $md5files{$file};
my $ctx = Digest::MD5->new;
open (my $fh, '<', $file) or die "Couldn't open data file '$file'\
+n";
$ctx->addfile($fh);
close ($fh);
my $line = sprintf "%s %s\n", $ctx->hexdigest(), $file;
print $line;
print MD5FILE $line;
}
close (MD5FILE);
__END__
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.