Cleaning your IMAP account

From openSUSE

The computing equivalent of a good broom for your IMAP account are your friends find, xargs, grep and rm. You need shell access to your IMAP account, which must be in Maildir-format. Maildir stores each email as a separate file in a directory.

Log into your account, change directory to where Maildir stores your current email

cd Maildir/cur/

We want to find these files, which might be in a subfolder, then pass the file to xargs which lets grep search for a pattern without the argument list becoming to long, then pass the hits to xargs which lets rm remove them. Like this:

find -type f -print0 | xargs -0 grep -liwZ "any character pattern you would like to find goes here" | xargs -0 rm -f

Example 1

find -type f -print0 | xargs -0 grep -liwZ "mailing-list@somewhere-you-do-not-subcribe-anymore.com" | xargs -0 rm -f

This will remove any email containing "mailing-list@somewhere-you-do-not-subcribe-anymore.com".

Example 2

find -type f -print0 | xargs -0 grep -liwZ "$$$ A dollar for a sigh" | xargs -0 rm -f

Well, you might not want to do this if you are down on your luck and want to make matters even worse.

Please type

man find
man xargs
man grep
man rm

to learn more about different options you might enjoy. Hint: you can use this to clean your home directory as well. This tip was originally found in The Advanced Bash-Scripting Guide at The Linux Documentation Project