OT: Removal of old 14+ mail from mbox-based mail spool (not maildir)

Giorgos Keramidas keramida at ceid.upatras.gr
Wed Aug 17 15:02:26 GMT 2005

On 2005-08-17 10:50, Matt Juszczak <matt at atopia.net> wrote:
> Hi all,
> Sorry this is off topic, just didn't really know where else to post
> this other than to fellow sys-admins.
> I need a script that will analyze a mail spool file and remove email
> from it that is more than 14 days old.  I found a couple; however,
> they require perl modules I couldn't seem to find. Does anyone have
> any ideas?  If not, I'll go ahead and write one.

If you have procmail, you can roll your own with something like:

	$ formail -s procmail procmailrc-remove-old < mbox

The ``procmailrc-remove-old'' ruleset can implement something like the
logic of removing old messages, by piping the messages one by one
through a shell script that uses date(1) on the envelope-from line.

All messages in a Unix mbox file start with a line like this:

	From keramida at FreeBSD.ORG Fri Sep  3 18:19:29 2004

You can extract the timestamp and convert it to seconds since the UNIX
Epoch time, with date(1):

% message_time=$( echo 'From keramida at FreeBSD.ORG Fri Sep  3 18:19:29 2004' | \
%     awk '{print $4,$5,$6,$7}' )
% message_seconds=$( date -j -f '%b %e %H:%M:%S %Y' "$message_time" '+%s' )

This should set $message_seconds to 1094224769.  Then you can get the
current time in seconds from the UNIX Epoch and perform ordinary numeric
operations, i.e. subtract the number of seconds in a 14-day period.

Messages with a $message_seconds value less than the current time minus
the period of your choise, should be thrown away.  The rest should be
echoed back to procmail, which will deliver them as usual.

This is just an idea of course, so you may want to look at existing
mailers, like Mutt, before you start scripting.  They usually include
options to select message ranges based on the arrival date.

- Giorgos

More information about the freebsd-questions mailing list