Hi!
Here's a script for you. USE AT YOUR OWN RISK! This script goes
under each subdirectory of /var/mail/ (assumed to be a mailbox), and
then deletes all the oldest emails past 5000 in count.
One caveat is that this script uses the mtime (last modified time)
when determining the age of the email. So its possible to circumvent
the whole system by going in and 'touching' the raw email file, and
thereby updating its mtime.
my $base = '/var/mail';
my $max_mail_items = 5000;
opendir MAILBOXES, $base;
### Get an array of mailboxes
my @mailboxes = grep { $_ ne '.' && $_ ne '..' } readdir MAILBOXES;
closedir MAILBOXES;
foreach my $mb (@mailboxes)
{
my $mailbox = "$base/$mb";
### Make sure we're only looking at directories
next unless -d $mailbox;
opendir MAIL, $mailbox;
### Read all the files in the mailbox, weed out '.' and '..', and sort by mtime
my @mail = sort { (stat "$mailbox/$b")[9] <=> (stat
"$mailbox/$a")[9] } grep { $_ ne '.' && $_ ne '..' } readdir MAIL;
### Pull out all the oldest mail over 5000
my @old_mail = splice(@mail, $max_mail_items + 1);
foreach my $mail (@old_mail)
{
### Delete it!
unlink "$mailbox/$mail";
}
} |