Google Answers Logo
View Question
 
Q: seeking the information of one script or software for deleting old emails ( No Answer,   1 Comment )
Question  
Subject: seeking the information of one script or software for deleting old emails
Category: Computers > Internet
Asked by: tonyleoliu-ga
List Price: $4.00
Posted: 25 Aug 2006 14:08 PDT
Expires: 24 Sep 2006 14:08 PDT
Question ID: 759562
hi gurus and guys!

I need a shell or perl script or small software which would keep
recent certain number of mails(say last 5000 mails) from mailbox of a
mail user account and delete all other older mails from its mailbox. I
m using UNIX OS, and have user mailboxes in /var/mail/*. Does anyone
of you have such a
shell or perl script or small software which would serve this purpose?
Or do you have this information?  Your help will be appreciated.

Thank you in advanced.
Answer  
There is no answer at this time.

Comments  
Subject: Re: seeking the information of one script or software for deleting old emails
From: jlavold-ga on 26 Aug 2006 22:51 PDT
 
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";
  }
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy