Koozali.org: home of the SME Server

Delete all mail containing certain words i subject field (from shell)

Offline tias_

  • *
  • 11
  • +0/-0
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

Offline jonic

  • *
  • 103
  • +1/-0
Re: Delete all mail containing certain words i subject field (from shell)
« Reply #1 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.

Offline tias_

  • *
  • 11
  • +0/-0
Re: Delete all mail containing certain words i subject field (from shell)
« Reply #2 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.

Offline Jáder

  • *
  • 1,099
  • +0/-0
    • LinuxFacil
Re: Delete all mail containing certain words i subject field (from shell)
« Reply #3 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!

...

Offline Curly

  • ****
  • 114
  • +0/-0
Re: Delete all mail containing certain words i subject field (from shell)
« Reply #4 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.
.......................................

Offline CharlieBrady

  • *
  • 6,918
  • +3/-0
Re: Delete all mail containing certain words i subject field (from shell)
« Reply #5 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. :-)

Offline tias_

  • *
  • 11
  • +0/-0
Re: Delete all mail containing certain words i subject field (from shell)
« Reply #6 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 :-)