Sample Header Ad - 728x90

Creating .spam folders in each mail user's account

0 votes
1 answer
280 views
My previous question, https://unix.stackexchange.com/questions/335842/cleaning-out-mail-folders-with-cron-task didn't result in a good enough answer to help me. I have narrowed down the task as follows: I wish to move mail out of each user's .spam/cur and .spam/new folders into the spam-teaching folder (by nightly cron job). There they will be processed and deleted. mv ~/mail/*/*/.spam/{cur,new}/* ~/mail/.sa-learn The above line generates an error if the folder doesn't exist. e.g., +-- mail +-- sitename.com +-- username1 | +-- .spam | +-- cur | +-- new +-- username2 <-- no ".spam" folder. +-- username3 | +-- .spam | +-- cur | +-- new Since it would be useful to create the folders for the users if they don't exist I am considering using touch to create them if they don't exist. Q1: Is the following approach robust enough? for dir in ~/mail/*/*/; do touch "$dir/.spam"; done for dir in ~/mail/*/*/.spam/; do touch "$dir/cur"; done for dir in ~/mail/*/*/.spam/; do touch "$dir/new"; done Q2: Will the move command now work without error even if the folders are empty? mv ~/mail/*/*/.spam/{cur,new}/* ~/mail/.sa-learn Q3: Is there a smarter way to do this? ---------- **Update 4** #!/bin/bash # SpamAssassin Learn script. # With help from Kusalananda's answer # to https://unix.stackexchange.com/questions/336412/creating-spam-folders-in-each-mail-users-account # # Any mail the user drops into their spam folder will be moved to a temp folder, # fed to SpamAssassin's sa-learn and then deleted. # The script also creates the .spam folders for each account if they don't already exist. # Run daily as a cron task. myDomain=sitename.com # Create .spam/, .spam/cur/ and .spam/new/ folders for each user. #mkdir -p "$HOME"/mail/"$myDomain"/*/.spam/{cur,new} for userdir in "$HOME"/mail/"$myDomain"/*; do test -d "$userdir" && mkdir -p "$userdir"/.spam/cur; done for userdir in "$HOME"/mail/"$myDomain"/*; do test -d "$userdir" && mkdir -p "$userdir"/.spam/new; done # Create a temp folder. mkdir -p "$HOME"/mail/.sa-learn/ # Find all the .spam emails and move them to the temp folder. # IMAP users should see their spam folder empty. find "$HOME"/mail/"$myDomain" -type f -path "*/.spam/cur/*" -print0 | xargs -0 -I XX mv "XX" "$HOME"/mail/.sa-learn/ find "$HOME"/mail/"$myDomain" -type f -path "*/.spam/new/*" -print0 | xargs -0 -I XX mv "XX" "$HOME"/mail/.sa-learn/ # Feed the emails into the SpamAssassin spam learner. sa-learn -p ~/.spamassassin/user_prefs --spam "$HOME"/mail/.sa-learn # Remove the temporary folder and its contents. rm -rf "$HOME"/mail/.sa-learn This works. I seemed to be having trouble with the {cur,new} syntax. User now drops spam into .spam folder. Cron job moves them to .sa-learn folder, feeds them to sa-learn and then deletes the folder.
Asked by Transistor (103 rep)
Jan 10, 2017, 08:34 PM
Last activity: Jan 12, 2017, 11:44 PM