Bash script to find && mkdir

1k views Asked by At

Scenario

I'm switching IMAP client on a mail server for several thousands domains with following structure :

Mail folder location: /home/mail/domains (just an example)

Maildir for domain1.com contains following folders: (again just an example)

.Sent.2002.May
.Sent.2002.June
.Personal.Home.Jack

Maildir for domain2.com contains

.Sent.2008.June
.Emails.Test.test2

Maildir for domain3.com contains

.Sent.2012.Files
.Archive.2014.May

Problem:

My new IMAP client throws an error whenever intermediate folder is not there. Example for domain1.com:

If .Sent.2002 folder doesn exist it will throw an error when I try to view .Sent.2002.May

Task

I need a script to:

  1. Find folders in /home/mail/domains/$domain/Maildir/ with this naming pattern .$1.$2.$3
  2. Create intermediate folders /home/mail/domains/$domain/.$1.$2/
  3. Chown /home/mail/domains/$domain/.$1.$2/ to user mail

Tried it manually for one domain and the IMAP client error is gone.

Need help with the script. I'm not sure how to translate arguments from find to mkdir in this case, because folder names always vary.

1

There are 1 answers

0
Breno Leitão On BEST ANSWER

You need something like:

for i in `find  /home/mail/domains/$domain/Maildir/  -exec basename {} \; ` 
    do
    DIR=`echo $i |  awk -F. '{print "/home/mail/domains/$domain/." $2 "." $3}'` 
    mkdir $DIR
    chown mail $DIR
done

Executing it here, I got:

mkdir /home/mail/domains/$domain/.Personal.Home
chown mail /home/mail/domains/$domain/.Personal.Home
mkdir /home/mail/domains/$domain/.Sent.2002
chown mail /home/mail/domains/$domain/.Sent.2002
mkdir /home/mail/domains/$domain/.Sent.2002
chown mail /home/mail/domains/$domain/.Sent.2002