Deleting sendmail file pairs based on content

This one-liner will identify files containing a string, and then use some bash string manipulation to generate a wildcard for the pair of files sendmail creates for each message (dfn for message content, and Qfn for headers). This assumes you’re running it in the mail queue directory (/var/spool/mqueue for example).

for f in `grep -l -i viagra *`; do t=${f:10:6}; rm -f *${t}; done

The ${f:10:6} extracts the last 6 characters of the filename, then the rm command prepends that with the wildcard.

Some sample output:

[root@mail mqueue]# grep -l -i cialis *

But there are actually six files:

[root@mail mqueue]# ls -l | egrep "020530|015512|031687"
-rw------- 1 root smmsp 2062 Aug 30 01:27 dfn7U8Rc3X020530
-rw------- 1 root smmsp 2232 Aug 30 06:07 dfn7UD7BUh015512
-rw------- 1 root smmsp 2069 Aug 31 07:21 dfn7VELCbV031687
-rw------- 1 root smmsp 825 Aug 30 01:27 Qfn7U8Rc3X020530
-rw------- 1 root smmsp 837 Aug 30 06:07 Qfn7UD7BUh015512
-rw------- 1 root smmsp 810 Aug 31 07:21 Qfn7VELCbV031687

So we run the command, using cialis:

for f in `grep -l -i cialis *`; do t=${f:10:6}; rm -f *${t}; done

Then there are no more files 🙂

[root@mail mqueue]# for f in `grep -l -i cialis *`; do t=${f:10:6}; rm -f *${t}; done
[root@mail mqueue]# grep -l -i cialis *
[root@mail mqueue]#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.