[lug] Virtual Mail User - Large Mailbox Scrubbing

David L. Anselmi anselmi at anselmi.us
Thu Jun 29 17:17:41 MDT 2006

Jeff Schroeder wrote:
> Ryan asked:
>> I would like to start by removing all messages that have
>> "Undeliverable" in the subject heading.
> The quick-and-dirty grep command would be something like this:
> for file in `grep -li "^Subject:.*undeliverable"`; do rm $file; done

This may not be quite right.  It will find messages matching your 
pattern on any line, not just the Subject header.  You can do perl (or 
if you have to, awk) to find matching headers and delete (the headers 
end at the first blank line).

Also, your backticks expand into all the matching files so the for 
command line might be too long anyway.  (Linux seems to accept really 
long lines compared to Solaris 8, but IIRC you can cause it to run out 
of memory before it errors on line length and that takes a while.)

> I believe that will work even for long lists of files; if you attempted 
> to just remove them at once using
> rm `grep -li "^Subject:.*undeliverable"`
> you may run into problems because the argument list to 'rm' will be too 
> long.  (Using xargs would solve that, but I actually don't know how to 
> use xargs, heh.)

ls | xargs grep -li "^Subject:.*undeliverable" | xargs rm

Heh, I've never used xargs twice in a row before.  You can use find 
(with no predicates) instead of ls if you want to process subdirectories.


More information about the LUG mailing list