Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say you have a directory with tens of thousands of messages in it. And you want to separate the spam from the non-spam.

Specifically, you would like to:

  1. Run spamassassin against the directory, tagging each message with an X-Spam-Flag: YES if it thinks it's spam
  2. Have a tcsh shell or perl one-liner grep all mail with the flag and move those mails to /tmp/spam

What command can you run to accomplish this? For example, some pseudocode:

/usr/local/bin/spamassassin -eL ./Maildir/cur/* | grep "X-Spam-Flag: YES" | mv %1 /tmp/spam
share|improve this question

Your first guess was pretty close :-)

I made a bash script to scan a whole maildir against spam



for f in $(ls $DIR); do
    spamassassin -Le "$DIR/$f" > /dev/null
    echo $ERR
    if [ $ERR -gt 0 ]; then 
        mv "$DIR/$f" /tmp/spam/
        echo "This was no spam."

Save this as somewhere, put the path of the folder you want to scan as DIR, set it +x and run it.

Good luck!

share|improve this answer
up vote 1 down vote accepted

Ended up doing this the long way, without using spamassassin:

  1. Download all headers via an IMAP client.
  2. Search headers for common spammy subjects and from addresses.
  3. Delete & purge spam en masse. (instead of moving to a new directory)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.