Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Simple tar-ing of files per month

by jkva (Chaplain)
on Feb 25, 2005 at 09:56 UTC ( [id://434416]=CUFP: print w/replies, xml ) Need Help??

I have some 20.000 .eml files that I wanted to be tar-ed by month. So I wrote a little script to do this. It took me about 4 hours, but I'm still a relative perl and Unix newbie. Nevertheless, I'm proud of it.
#!/usr/bin/perl -w #### #### Über script dat .eml files naar tarballen omzet :) #### 240205 #### #### array voor maandnaam toevoeging use strict; use warnings; my $i = 0; my @files = (); my %opties = ( 'filedir', '/usr/local/assp/spam/', 'archdir', '/usr/mail/tar/', 'fext', '.eml' ); foreach (`ls -lT $opties{filedir}`) #make a list of the files {push @files,$_ if /.$opties{fext}$/;} #only add e-mail files foreach(@files) { $i++; my @data = split(/\s+/,$_); #remove whitespace `tar --append --file $opties{archdir}$data[5]$data[8].tar $opties{fil +edir}$data[9] &>/dev/null`; print "$i of ".scalar(@files)."\n"; } #tar the file into an archive by month and year (ex : Dec2004.tar) 1;

Replies are listed 'Best First'.
Re: Simple tar-ing of files per month
by jkva (Chaplain) on Feb 27, 2005 at 17:31 UTC
    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.

    1
Re: Simple tar-ing of files per month
by graff (Chancellor) on Feb 27, 2005 at 02:07 UTC
    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.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://434416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-29 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found