5

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?

Rich
  • 12,068
  • 9
  • 62
  • 94

4 Answers4

2

Could be something like this:

DIR=
for i in a b c d e f; do
    DIR=$DIR$i/
    chmod a+x $DIR
done
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
1

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
  • This is what `chmod -r` does (initially when the directories are empty). – Diego Sevilla Aug 29 '11 at 22:05
  • Diego, please see second part and third (just added) part of answer. – Richard Aug 29 '11 at 22:10
  • Well, I don't like this one very much for several reasons. First, you use `$*`, which means that the script has to be called with the directories separed by spaces (you don't specify that). Also, you *change the directory* in each step of the loop. This is considered a too big change of the state of the application/environment, because, for example, at the end of the loop, in which directory are you? Finally, you don't have to change the dir if you can do it from the actual directory, along the lines of what I do in my response. – Diego Sevilla Aug 30 '11 at 10:15
  • @Diego, I've more clearly specified the need for spaces. I think Rich was probably in search of a general answer, so I feel `$*` is a good choice here. When you run a script it forks into a separate process which inherits, but does not edit, the original environment (see, for instance, http://stackoverflow.com/questions/874452/change-current-directory-from-a-script); if you test the script (I did), you'll find this to be true. Your worry that we're in a random directory when the script ends is misplaced. – Richard Aug 30 '11 at 19:13
0
#!/bin/sh

perm=$1
shift

# iterate over all argument paths
for p; do
    # chmod up successively from destination 
    while true; do
        case $p in
            */* ) chmod "$perm" "$p"
                   p=${p%/*} ;;
            * ) break ;;
        esac
    done
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Of course, this has a problem if you are adding write access where previously you didn't have write access, because it starts from the target and works is way up the path. It is certainly possible to fix this, but it will add some complexity, so I won't. – tripleee Aug 31 '11 at 07:33
-1

you can use find command

find /path_at_remote -type d -exec chmod a+x "{}" \;  (not tested)
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • There's nothing wrong with this approach, just put it at the end of an ssh command and you should be good to go. watch out if you got directories with spaces in them, then you probably want to avoid the -exec and instead of do `find /somedir -type d -print0 | xargs -0 chmod a+x `. – Marcin Aug 30 '11 at 20:30
  • Nah..it will work just fine with -exec and "{}" for directories with spaces. With GNU find, there is also no need to use `xargs` – ghostdog74 Aug 31 '11 at 01:22
  • 1
    This `chmod`s all the directories below `/path_at_remote`. I'm interested in only chmoding the directories belonging to a particular path like `/a/b/c/d/e/f`. – Rich Aug 31 '11 at 16:15