My previous question, 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-learnThe 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    |   +-- newSince 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; donefor dir in ~/mail/*/*/.spam/; do touch $dir/cur; donefor dir in ~/mail/*/*/.spam/; do touch $dir/new; doneQ2: Will the move command now work without error even if the folders are empty?mv ~/mail/*/*/.spam/{cur,new}/* ~/mail/.sa-learnQ3: 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; donefor 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-learnThis 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.
Creating .spam folders in each mail user's account
shell;touch;spamassasin
You can not create a directory with touch, use mkdir for this:mkdir -p $HOME/mail/.sa-learn/{cur,new,tmp}The -p flag tells mkdir to create all intermediate folders if they do not exist, and makes it not an error to try to create a folder that already exist.That is assuming a shell that does brace expansion. If it doesn't, break it up inte three separate mkdir invocations.When moving mail (untested):find $HOME/mail -type f -path */.spam/* -print0 |xargs -0 -I XX mv XX $HOME/mail/.sa-learn/new/This will find all mail (new or old) in any .spam folder under $HOME/mail and move them into the .sa-learn/new folder.I'm using $HOME rather than ~ in scripts because I think it looks nicer, it's self-documenting, and it behaves as any other variable (~ doesn't).