1

Some automatic processes, like those made at regular times using cron, generate a lot of messages. I would like to delete automatically those older than a month so my mailbox doesn't grow too much.

I tried with a bash script that archives the old messages and tries to delete them from the mailbox, but I get this message from the system:

Mailbox /var/spool/mail/username was modified or is in use now.
It's not safe to complete the operation...

I have the same problem with both exim and postfix (in different servers).

I guess I could do it simulating a local email client like mutt using bash or python but I am not sure how to do it. I am not interested in using maildir because there are thousands of small messages that are more manageable in a single mailbox.

This is a local server using mail spool and local clients. We are not using imap or sending messages outside the server.

I will appreciate any hints.

1 Answers1

1

The grepmail command can do what you want. You may also be able to accomplish this using just the mail[x] command with some ingenuity. I would suggest something like:

mailx <<EOF
s 1-$ mbox
q
EOF

grepmail ...

This will save all messages in your system mailbox to a file called mbox. It will also delete those messages from your system mailbox. You can then run grepmail on the mbox file.

Romeo Ninov
  • 5,263
  • 4
  • 20
  • 26
  • Thanks, the problem is that I need to do this automatically from a script. If I put your mailx commands in a bash script it doesn't recognize s as a command but as a syntax error , and fails. – user2309000 Aug 20 '22 at 01:10