mailtrim - Deletes old emails from the named unix mailbox file. Use at your own risk


mailtrim [-d days] [-x] mailbox


mailbox is name of the mailbox file to be trimmed.

-d days : delete all messages more than days old, i.e.days is the maximum age, in days, of a message that will be allowed in the file

-d 0 will keep only today's messages

if omitted, -d 14 is assumed (delete messages more than 14 days old)

-x : if omitted discarded emails will be sent to STDOUT


This script was written to empty old mail out the mailboxes of CUUG members who just never got around to doing tyhe job themselves. One user had over 250mb of mail in his shell account!

It has only been tested at CUUG, using our postfix mail server. You may need to make minor adjustments to make it work with your server... probably to the date format in the first line of each message


  1. delete everything more than a month old from joeuser's "mbox"
    and put the discarded old messages into a gzipped archive

    cd ~joeuser
    mailtrim -d 31 mbox | gzip > oldmail.gz

  2. delete everything but today's mail from joeuser's "mbox";
    don't show the discard messages

    cd ~joeuser
    mailtrim -d 0 -x mbox


Perl 5, Getopt::Std


Mike Babulic,


This program is distributed under the Perl Artistic License.


mailtrim was last changed on June 29, 2004. You can find it on the author's website

gzip format :

zip format :