#!/usr/bin/env perl ############################################################################### ### Declarations ############################################################## ############################################################################### use strict; use File::Copy; use POSIX qw/strftime/; ############################################################################### ### main () ################################################################### ############################################################################### my $dir = shift @ARGV || "."; opendir (DIR, $dir) or die "Couldn't open $dir: $!\n"; my @files = readdir (DIR); closedir DIR; my $current = timestamp (time); foreach my $file (sort @files) { next unless $file =~ /^(.*)?[\-\.]?(\d\d\d\d)-?(\d\d)-?(\d\d)(\.\w+)?$/; my $shortdir = "$2/"; my $newdir = "$2/$2-$3"; # skip current date next if ("$2-$3-$4" eq $current); # make the directory if necessary unless ( -d $newdir ) { unless ( -d $shortdir ) { mkdir ($shortdir) or die "Couldn't create $shortdir: $!\n"; } mkdir ($newdir) or die "Couldn't create $newdir: $!\n" ; } move ($file, "$newdir/$file") or die "Couldn't move $file: $!\n"; print "Moved $file to $newdir/$file\n"; } exit 0; ############################################################################### ### Subroutines ############################################################### ############################################################################### sub timestamp { my $time = shift || time; strftime ('%Y-%m-%d', localtime ($time)); } ############################################################################### ### Documentation ############################################################# ############################################################################### =head1 NAME mailmove - move old mail files into a consistent name structure =head1 SYNOPSIS B B I =head1 DESCRIPTION mailmove manages a directory structure for old mail logs and the like. When run against a directory with files named after the date (YYYY-MM-DD), then those files are moved into a directory structure like this: 2008/2008-12/2008-12-15 2008/2008-12/access_log.2008-12-15.gz Note that the current date's files are not moved, so that we can continue to write to that file during the day. Defaults to run on the current directory. =head1 AUTHOR Tim Skirvin =cut