How to use bash to run `mkdir -m -p`?

Viewed 536

I want to mkdir as:

site1/www,site1/log
site2/www,site2/log
site3/www,site3/log

And permission of these folders is 700.

Then I tried script in Bash shell as below:

sites_arr=(site1 site2 site3)
        for sitename in ${sites_arr[@]}
        do
                mkdir $sitename
                mkdir –m 700 –p /var/${sitename}/{www/,log/}

        done

But mkdir –m 700 –p ${sitename}/{www/,log/} always give me error:

mkdir: cannot create directory ‘–m’: File exists
mkdir: cannot create directory ‘700’: File exists
mkdir: cannot create directory ‘–p’: File exists

Where is the problem?

1 Answers

You are using the wrong type of dash character. You typed an en dash

mkdir –m 700 –p /var/${sitename}/{www/,log/}

but options are specified with a hyphen -.

mkdir -m 700 -p /var/${sitename}/{www/,log/}

This is probably the result of copy/paste magic with some word processors.

With the wrong type of dash, what you typed as options are interpreted as multiple arguments for mkdir, which then tries to create a directory named –m, one named 700 and one named –p. This of course fails the second time the command is executed since those directories were already created.

Related