Hi All,
I want to know how to grep and delete (remove) files .
What I need is that there are some unwanted out going mails in the queue in my Centos 4.4 Sendmail Server. I can find them with the below commad.
mailq /var/spool/mqueue/ |grep '<>'
Then It displays all the mails of that type withg their ids in the following way.
[root@gateway ~]# mailq /var/spool/mqueue/ |grep '<>' kA63ZwJT007450 13864 Mon Nov 6 09:05 <> kA63LqO3004442 4110 Mon Nov 6 08:51 <> kA63V7Tn006327 16913 Mon Nov 6 09:01 <> kA62vH4t031111 13880 Mon Nov 6 08:27 <> kA62r6WW030077 13975 Mon Nov 6 08:23 <> kA63BxYQ002455 16443 Mon Nov 6 08:41 <> kA62MW0w023058 13536 Mon Nov 6 07:52 <> kA62jQah028236 13625 Mon Nov 6 08:15 <> kA62gLTw027445* 13710 Mon Nov 6 08:12 <> kA62JXwd022569 13849 Mon Nov 6 07:49 <> kA62idkg028035 13949 Mon Nov 6 08:14 <> kA62PEHX023554* 14152 Mon Nov 6 07:55 <> kA62W7Dd024920 14203 Mon Nov 6 08:02 <> kA62TtJn024569 14245 Mon Nov 6 07:59 <> kA62TU2j024446 14262 Mon Nov 6 07:59 <> kA62ZL0E025726 15878 Mon Nov 6 08:05 <> kA62MW6J023059 17106 Mon Nov 6 07:52 <> kA6266rV019846 13678 Mon Nov 6 07:36 <> kA62FXCd021741 13693 Mon Nov 6 07:45 <> kA626v5u020008 13718 Mon Nov 6 07:36 <> kA62X36L025101 13873 Mon Nov 6 08:03 <> kA62Xd6u025234 13901 Mon Nov 6 08:03 <> kA62RMT8023973 14008 Mon Nov 6 07:57 <> kA62Ufi6024703 14014 Mon Nov 6 08:00 <> kA62JxSm022664 14044 Mon Nov 6 07:49 <> kA62Q0Sd023738 14057 Mon Nov 6 07:56 <> kA62VgKI024882 14116 Mon Nov 6 08:01 <> kA62QmtQ023890 14129 Mon Nov 6 07:56 <> kA623NxG019271 13740 Mon Nov 6 07:33 <> kA620tLk018801 14004 Mon Nov 6 07:30 <> kA61q1PS017035 14094 Mon Nov 6 07:22 <> kA6215S7018835 14154 Mon Nov 6 07:31 <> kA61kYX8015811 13536 Mon Nov 6 07:16 <> kA61c63T014124 13811 Mon Nov 6 07:08 <> kA61WhXL013089 10232 Mon Nov 6 07:02 <>
Now I want to know how to remove them.
Then I can set up a crontab to do it evey 5 minutues.
Any help .. pls
On Mon, 6 Nov 2006, Indunil Jayasooriya wrote:
Hi All,
I want to know how to grep and delete (remove) files .
What I need is that there are some unwanted out going mails in the queue in my Centos 4.4 Sendmail Server. I can find them with the below commad.
mailq /var/spool/mqueue/ |grep '<>'
Then It displays all the mails of that type withg their ids in the following way.
[root@gateway ~]# mailq /var/spool/mqueue/ |grep '<>' kA63ZwJT007450 13864 Mon Nov 6 09:05 <>
[root@gateway ~]# mailq /var/spool/mqueue/ |grep '<>' | awk '{print $1}' | xargs rm -f
you could eliminate the grep by using awk's pattern matching, but will have to figure out the escapes. Can also replace awk with sed, perl or cut.
------------------------------------------------------------------------ Jim Wildman, CISSP, RHCE jim@rossberry.com http://www.rossberry.com "Society in every state is a blessing, but Government, even in its best state, is a necessary evil; in its worst state, an intolerable one." Thomas Paine
On Sunday 05 November 2006 19:48, Indunil Jayasooriya wrote:
Hi All,
I want to know how to grep and delete (remove) files .
What I need is that there are some unwanted out going mails in the queue in my Centos 4.4 Sendmail Server. I can find them with the below commad.
mailq /var/spool/mqueue/ |grep '<>'
Then It displays all the mails of that type withg their ids in the following way.
[root@gateway ~]# mailq /var/spool/mqueue/ |grep '<>' kA63ZwJT007450 13864 Mon Nov 6 09:05 <> kA63LqO3004442 4110 Mon Nov 6 08:51 <>
<snipped>
Now I want to know how to remove them.
Then I can set up a crontab to do it evey 5 minutues.
Any help .. pls
You probably are looking for xargs. You can pipe a list of files to it to have it run them as arguments to another command. e.g. for example, "ls /dir/with/lots/of/files | xargs -n100 rm" would run rm on the output of the ls, with 100 (without the -n, it passes them all in one command) files passed as an argument at a time (helpful for when a directory has more files than rm likes as arguments).
To accomplish what you are looking for with the above command, you probably want to pass it through sed and remove everything but the message ID, and then you have to prepend a qf and a df to match the two files that message ID corresponds to in /var/spool/mqueue. The following should get you on your way: mailq /var/spool/mqueue/ | grep '<>' | sed 's#^([a-zA-Z0-9]+).*#/var/spool/mqueue/df\1\n/var/spool/mqueue/qf\1#g'| xargs ls
That said, I would carefully review what you are doing and make sure that this will only affect files that you want to remove. Something a bit less invasive might be to quarantine the messages instead of deleting them, and couple the quarantine (just a move to some other directory created for that purpose) with another cron that deletes messages in that folder after a certain time period (such as "find /quarantine/dir -maxdepth 1 -type f -ctime +30 | xargs rm -f").