Automatically delete emails matching the string "lan.scan@"
(I am a beginner at Linux and cPanel) My main goal is to make a cron job or script or command that allows me to automatically delete spam emails older than 2 weeks with all emails that start with "scan" and I have seen in other forms commands such as
Code:
Spam = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.spam/ This allows me to only delete 1 specific emails spam folder at but I want to be able to delete all spam folders under all emails that start with "scan" and I could either use cron job or make a script to run in the terminal. Any help is appreciated, thank you.
Spam = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.spam/ This allows me to only delete 1 specific emails spam folder at but I want to be able to delete all spam folders under all emails that start with "scan" and I could either use cron job or make a script to run in the terminal. Any help is appreciated, thank you.
-
Hey there! While I don't have a ready-made script for this, you'll likely want to use something similar to this to get all the mail directories: find /home/*/mail/ as the "find" command can use wildcards to get information across multiple accounts. 0 -
Hey there! While I don't have a ready-made script for this, you'll likely want to use something similar to this to get all the mail directories: find /home/*/mail/ as the "find" command can use wildcards to get information across multiple accounts.
I have looked into that command but how would you for example make it find all matching email addresses that match the string "scan" for example instead of the focus on 1 email. Thank you for the reply.0 -
Find does all that work for you. For example, this works well to find all the .spam folders on the system: # find /home/*/mail/*/ -type d -name ".spam" /home/user1/mail/domain1.com/email1/.spam /home/user1/mail/domain1.com/email2/.spam /home/user2/mail/domain2.com/email1/.spam /home/user3/mail/domain3.com/email1/.spam /home/user4/mail/domain4.com/email1/.spam 0 -
Find does all that work for you. For example, this works well to find all the .spam folders on the system: # find /home/*/mail/*/ -type d -name ".spam" /home/user1/mail/domain1.com/email1/.spam /home/user1/mail/domain1.com/email2/.spam /home/user2/mail/domain2.com/email1/.spam /home/user3/mail/domain3.com/email1/.spam /home/user4/mail/domain4.com/email1/.spam
Thank for the example as it helped me find the exact locations for each file. The code below works with deleting each emailfind /home/%user/mail/example.com/mailuser/.spam -type f -mtime +14 -exec rm {} \;
I can now do this for each email through cron job but if possible is there any way where instead of putting the exact "mailuser", I can do this command of deleting spam folders for specific set of emails that start with "string". An example of what I mean is if I have 5 deferent emails such as link.email1@domain.com link.email2@domain.com link.email3@domain.com testing@domain.com testing@domain.com Am I able to find all email that start with "link" and delete the spam folder of those emails every 2 weeks? or if there is a way to make a script that will find all matching emails with the string "link" and then the code above can run and delete spam (or other types of folders) folders every 2 weeks? Thanks.0 -
I'm not sure there's going to be an easy way to pull specific messages by subject or address, as they don't get stored with a helpful filename. 0 -
I'm not sure there's going to be an easy way to pull specific messages by subject or address, as they don't get stored with a helpful filename.
Ok, I will try to look around for such a command and if I find a solution to that, I will post it here. Thank you.0 -
Ok, I will try to look around for such a command and if I find a solution to that, I will post it here. Thank you.
I have found a way to find all emails and delete "Sent" emails after a certain amount of time with the code belowfind /home/user/mail/domain/scan*/.Sent -type f -mtime +14 -exec rm {} \;
This allows me to delete all the sent emails older than 2 weeks on every emails that matches (or starts) with "scan". The only issue I am having now is that my junk emails are not getting deleted with using this codefind /home/user/mail/domain/scan*/.Junk -type f -mtime +14 -exec rm {} \;
While testing the codes I tried to move a email from my inbox to junk and run the code but It would not do anything. Thank you.0 -
Try running the commands without the "-exec rm {} \;" portion of the code as that will just print a list of results. You can then modify the command until you get the expected results before passing the "rm" portion. 0
Please sign in to leave a comment.
Comments
8 comments