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").