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:
(One slight difference relative to the OP: in this version, the tar files don't store the absolute path to each file.)#!/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"; }
In reply to Re: Simple tar-ing of files per month
by graff
in thread Simple tar-ing of files per month
by jkva
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |