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.)

In reply to Re: Simple tar-ing of files per month by graff
in thread Simple tar-ing of files per month by jkva

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.