Koozali.org: home of the SME Server

Obsolete Releases => SME Server 7.x => Topic started by: tias_ on September 03, 2007, 03:57:38 PM

Title: Delete all mail containing certain words i subject field (from shell)
Post by: tias_ on September 03, 2007, 03:57:38 PM
Hi, I have a question about erasing multiple mails. My account contains a large number of mail which I want to sort out. What I hope I can do is to have Linux command that deletes every mail that contains certain words in the subject.

So the question is, do someone know how to solve this from a shell?

Best regards,
//Tias
Title: Re: Delete all mail containing certain words i subject field (from shell)
Post by: jonic on September 03, 2007, 04:15:16 PM
Don't know about the shell, but you could use Thunderbird and set up your account via imap. There is a search box in thunderbird that will easily show all your messages that match your criteria. Then delete them and they will also be deleted on the server.
Title: Re: Delete all mail containing certain words i subject field (from shell)
Post by: tias_ on September 03, 2007, 04:38:03 PM
tried that but Thunderbird are not that happy with the mission. We talk about thousands of mail.
A clean up process.
Title: Re: Delete all mail containing certain words i subject field (from shell)
Post by: Jáder on September 04, 2007, 05:19:06 PM
I have no tryed this... so be cautelous!
Code: [Select]
cd /<your_full_of_email_directory>
find <something_to_find> -exec rm -f {} \;

PLEASE... teste it with a minor search <something_to_find> first... and BE SURE TO BE ON RIGHT DIRECTORY!

Title: Re: Delete all mail containing certain words i subject field (from shell)
Post by: Curly on September 04, 2007, 08:35:50 PM
Find only works on the names, so something like:

rm -f `find . -exec grep -l SearchWord {} \; `

but to be sure, just test with

find . -exec grep -l SearchWord {} \;

That should give the names of the files you would want to delete. (tested this)

Of course, if you do this wrong you could break the system, so make a backup first. Check the directory where you execute this command.
Title: Re: Delete all mail containing certain words i subject field (from shell)
Post by: CharlieBrady on September 04, 2007, 09:00:08 PM
but to be sure, just test with

find . -exec grep -l SearchWord {} \;

That should give the names of the files you would want to delete. (tested this)

That is a very slow way to do it. Use 'grep -r ..." instead, and grep will only be started once, not once per email message.

Code: [Select]
grep -r -l 'Subject:.*text you are looking for' Maildir/cur

will list the filenames of each of the messages you want to remove.

Code: [Select]
grep -r -l 'Subject:.*text you are looking for' Maildir/cur | xargs rm -f

will remove all those messages.

Don't blame me if you try this and it doesn't do exactly what you want it to do. :-)
Title: Re: Delete all mail containing certain words i subject field (from shell)
Post by: tias_ on September 04, 2007, 11:09:15 PM
Well, I might have to drop a post here so I don't get too much tips. Probably would have solved my problem, some how it managed it self. Thunderbird must have worked for most of the mails through imap. After that pop3 started to work and I simply downloaded the rest.

Thanx for your advice, even though I didn't use them. But learned a little bit more about grep :-)