You've used an approach to write it which resembles mostly my original idea of how the script should be, but my knowledge fell short. This is still more efficient than my first would-be version though. But as I learnt in a previous discussion, if it is made in a short time period (~4 hours in my case) it is better than spending more time making it efficient.
Thanks though. I'm gonna make good use of this code.
| [reply] [Watch: Dir/Any] [d/l] |
Was the four hours referring to how long it took to write it, or how long it took to run? Whatever the actual run-time was, it would have been shorter if you had done only one tar job per MonthYear data set, instead of a separate job for each single file (20,000 of those... that's a lot of OS overhead to launch subshells, run tar, open files, read/write, close files, shut down subshells).
Tying the process to the output of "ls -lT" tends to make it system-dependent (e.g., on my system, I would find month at $data[6] instead of $data[5]).
Also, when using date strings as file names, I like to put the year first, then the month number, so it's easier sort file names chronologically.
There's actually an Archive::Tar module that you can use to create tar files within a perl script -- a handy thing if you ever need to do this on a non-unix machine where tar might not be available. Still, since you have unix tar, just make an inclusion list for each monthly tar file, and run tar once to create each file.
Here's another version of your script, which will probably run a lot quicker:
#!/usr/bin/perl
use strict;
# create monthly tar files from a directory full of *.eml files
my %opties =
(
'filedir', '/usr/local/assp/spam',
'archdir', '/usr/mail/tar',
'fext', '\.eml$'
);
chdir $opties{filedir};
my ( %files, %tarsets );
# %files: keys = file names; values = file modification date
# %tarsets: keys are "YYYYMM", values are lists of data file names
opendir D, ".";
for ( grep /$opties{fext}/, readdir D ) {
$files{$_} = $^T - ((-M)*24*3600);
# convert file age to seconds, subtract from $^T to get file mod.date,
}
closedir D;
for my $f ( keys %files )
{
# use localtime to get month, year of modification:
my ($m,$y) = (localtime($files{$f}))[4,5];
my $ym = sprintf( "%d%02d", $y+1900, $m+1 );
$tarsets{$ym} .= "$f\n";
}
# run tar with "--files-from -" and print file name list to it thru a
+pipe
$|++;
for my $d ( sort keys %tarsets )
{
print "makeing tar file for $d...";
open T, "| tar --create --file $opties{archdir}/$d.tar --files-from
+ -"
or die "can't run tar for $d: $!";
print T $tarsets{$d};
close T;
print "done\n";
}
(One slight difference relative to the OP: in this version, the tar files don't store the absolute path to each file.) | [reply] [Watch: Dir/Any] [d/l] [select] |