Tuesday, August 21, 2007

delete spam email and folder regularly howto

Fighting spam email is a worldwide daily combat challenge. Email spam fight is just another daily server wide monitoring function of any sysad administering those email servers. Global spam email attacks and happens everyday regardless of country, server setup, domains, geolocation and public IP address you might have. Take a look of the top country source of spam emails from here.

This entry covers how to delete bulk or spam folder of mbox/maildir/mdir type emails on regular monthly basis. This would be done using linux find command, delete command, and crontab utility statements via terminal.

Assuming the script would be created by root. Launch your fave editor and create a sample script delete_spam.sh with similar contents like so:


find /home -name Spam -exec rm -f {} \;

The 'find' linux command attempts to locate mbox file type named 'Spam'. Locating the file started from /home folder and done recursively. If a file type named Spam is found, rm -rf forcefully deletes the file and proceed with the next search result until all directory folders have been traversed.

Why home? This blog entry has assumptions that all spam mail files or folders are all stored under each user's home folders located under /home.

Why search for mbox type file only?

If you wish to delete IMAP folders, or any user folder, replace the find command with

# find /home -name myimapfolder -type d -exec rm -f {} \;

Make them root executable like so.

# chmod 700 delete_spam.sh

Now, have a crontab schedule with crontab utility. The script would be executed on regular monthly basis, for example like so:

33 01 28 * * /root/scripts/delete_spam.sh > /dev/null 2>&1

delete_spam.sh is executed once every 1:33AM every 28th of the month without any history logs.

Why not 30th of the month? Because of February month.

Some servers use IMAP folders or maildir type of emails. On those ones, you just need to fine tune and adjust file name search criteria. This can be done by specifying a folder instead of a filename as a search criteria. Here are more samples.

find /home -name Spam -exec rm -f {} \;
find /home -name Bulk -exec rm -f {} \;
find /home -name Virus -exec rm -f {} \;
find /home -name Spam.db -exec rm -f {} \;
find /home -name Spam.cache -exec rm -f {} \;
find /home -name spam-mail -exec rm -f {} \;
find /tmp -name att* -exec rm -rf {} \;
find /tmp -name *.tmp -exec rm -rf {} \;
find /home -name mymaildir -type d -exec rm -rf {} \;
find /home -name myfoldername -type d -exec rm -rf {} \;

Take note that, it is advisable that all email users must be well informed that these spam emails and/or spam folders are deleted regularly on a monthly basis as shown with above script examples.

That is all folks.


Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy