I would like to create a directory using a bash script and then set the mode to 00755 at the same time

mkdir -p -m=00755 "/dir/dir2" 

Is this the correct way of using them together and can I also add chown command to the same line while creating them?

3 Answers

It goes a little like this:

install -d -m 0755 -o someuser -g somegroup /dir/dir2 
5

If you want to set the owner during creation, you can simply impersonate as this user, using sudo for example:

sudo -uTHE_USER mkdir -p -m=00755 "/dir/dir2" 

This has the advantage that there will be no time difference between creation and changing the ownership, which could otherwise being harmful if exploited.

3

Yes that should work. As for the chown, simply follow the command ' && chown... '. && is similar to ; except the next command ONLY executes if the previous command exits success (0).

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy