Skip to content

How to set permissions for every directory along a directory path

An answer to this question on Stack Overflow.

Question

I'm creating a remote directory path on a webserver that I'd like to be executable. I create it remotely with a command like:

ssh user@machine mkdir -p a/b/c/d/e/f

Next, I'd like to be able to chmod directories a-f but avoid doing a chmod -r on the remote directory root.

Is there an elegant mechanism to start with the a/b/c/d/e/f path and do an effective chmod a+x a; chmod a+x a/b; chmod a+x a/b/c; ... without parsing out each chmod?

Answer

One think you might try would be to make a remote bash script as follows (directory names should be separated by spaces):

#!/bin/bash
for i in $*
do
  mkdir $i
  chmod a+x $i
  cd $i
done

Or you could set umask as umask 066 which will set the default permissions of anything you create. See this site for an explanation.