set read and write permissions to folder and all its parent directories

15,379

Solution 1

This can be done easily in the shell, starting in the subdir and moving upwards:

f=/root/subfolder1/subfolder2/subfolderN
while [[ $f != / ]]; do chmod +rx "$f"; f=$(dirname "$f"); done;

This starts with whatever file/directory you set f too, and works on every parent directory, until it encounters "/" (or whatever you set the string in the condition of the loop to). It does not chmod "/". Make sure both f and the directory in the condition of the loop are absolute paths.

Solution 2

With csh, tcsh, ksh, zsh, bash, fish or yash -o braceexpand:

sudo chmod +rx /root{,/subfolder1{,/subfolder2{,/subfolderN}}}

With zsh:

f=/root/subfolder1/subfolder2/subfolderN
until [[ $f = / ]] {chmod +rx $f; f=$f:h;}

Or you could define a glob qualifier function like:

explode() {
  reply=()
  until [[ $REPLY = [./] ]] {
    reply+=$REPLY
    REPLY=$REPLY:h
  }
}

To be used for instance as:

$ echo chmod +rx subfolder1/subfolder2/subfolderN(+explode)
chmod +rx subfolder1 subfolder1/subfolder2 subfolder1/subfolder2/subfolderN  

Note that chmod +rx is affected by the umask. If your umask doesn't include 007, it would make the /root directory world-readable and accessible which is a bad idea. /root is typically for the super-user's private things, it's a bad idea to expose it.

Solution 3

I don't know what you are trying to do, but is better than you don't take the recursive lightly. That said, read the actual answer:

Umm... why not just use recursive.

sudo chmod -R +rx /root

Or if you don't like it, you can give chmod several directories:

sudo chamod +rx /root /root/subfolder1 /root/subfolder1/subfolder2 /root/subfolder1/subfolder2/subfolderN

Solution 4

Well, you could do something slightly more complex like:

echo "/root/subfolder1/subfolder2/subfolderN" | 
 perl -anF'/' -e 'while($#F>0){@b=join("/",@F);`chmod +rx @b`; pop @F}' 

To see what this will do, replace the chmod call with print:

$ echo "/root/subfolder1/subfolder2/subfolderN" | 
 perl -alnF'/' -e 'while($#F>0){@b=join("/",@F);print "chmod +rx @b"; pop @F}' 
chmod +rx /root/subfolder1/subfolder2/subfolderN
chmod +rx /root/subfolder1/subfolder2
chmod +rx /root/subfolder1
chmod +rx /root
Share:
15,379

Related videos on Youtube

Maxim Yefremov
Author by

Maxim Yefremov

Updated on September 18, 2022

Comments

  • Maxim Yefremov
    Maxim Yefremov over 1 year

    I need to set read and write permissions for root user to directory subfolderN and all its parent folders till root.

    I can do it by hands:

    $ sudo chmod +rx /root/subfolder1/subfolder2/subfolderN
    $ sudo chmod +rx /root/subfolder1/subfolder2
    $ sudo chmod +rx /root/subfolder1
    $ sudo chmod +rx /root
    

    But if N is big I am tired. How to do automatically by one command?

    • Stéphane Chazelas
      Stéphane Chazelas almost 6 years
      You should really not make /root world readable.
  • terdon
    terdon over 10 years
    Nice, I knew I was complicating things needlessly with Perl.
  • 200_success
    200_success over 10 years
    That's not nearly the same operation, though. chmod -R /root changes everything under /root — including regular files and all directories. The question is about changing one chain of directories only.
  • Ludwig Schulze
    Ludwig Schulze over 10 years
    @200_success but that's not the only answer ;)
  • terdon
    terdon over 10 years
    Could you explain what the leading , in {,/subfolder} does?
  • tripleee
    tripleee almost 6 years
    It creates an alternation where the first string is empty and the second is /subfolder; so /root{,/subfolder} produces /root and /root/subfolder
  • tripleee
    tripleee almost 6 years
    Notably this is not portable to POSIX sh
  • Tim
    Tim almost 6 years
    But what if f is a relative path, and I want to stop at what it is relative to instead of /?
  • Tim
    Tim almost 6 years
    @StéphaneChazelas Thanks. If $f is foo/bar, will $f ever change to .? Yes dirname does it. Thanks
  • jmc
    jmc over 5 years
    The question is not about recursion, but how to set permissions on a chain of parent folders to allow selective access to a subfolder. Saying that it's not the only answer is like saying driving a bus through your wall is another way to make a door.
  • Felipe Rodriguez
    Felipe Rodriguez about 5 years
    @Tim convert relative path to absolute path before storing in f using command "f=`readlink -f <relative_path>`"
  • user3282611
    user3282611 about 4 years
    Remember to use absolute path. I used short link path and had a lot problems. Finally get it back to normal.
  • Admin
    Admin about 2 years
    I had to remove the quotation marks to make it work for my case: f=/root/subfolder1/subfolder2/subfolderN; while [[ $f != / ]]; do chmod +x $f; f=$(dirname $f); done;