1

There is the following directory tree in several of my Linux boxes:

/dir1/dir2/dir3/dir4/target

Each of the dir[1-4] directories have several directories under them and all these directories (and its sub-directories) have 750 permissions. Now, I want to others to browse through the 'target' directory and its sub-directories. Running chmod -R o+rx on /dir1/dir2/dir3/dir4/target, does change the permission on /dir1/dir2/dir3/dir4/target to 755, but the others users cannot go to that directory since there is no r-x permission on its parent directories. On one of the servers, I worked around this by granting the following:

chmod -R o+x /dir1
chmod -R o+rx /dir1/dir2/dir3/dir4/target

Considering that the above command would give 'traverse' permission on all the subdirectories of /dir[1-4] too, is there a better command to provide traversal permissions on only /dir[1-4] directories and r-x permission on the 'target' directory?

  • You've just given everyone the permission to execute every file in the /dir1 directory (and below) as well. – Kusalananda Jul 9 '18 at 9:52
3

To set x permissions for "others" on /dir1/dir2/.../target and all intermediate directories (except for the root directory), and r permissions on the target directory at the end of the path:

dir=/dir1/dir2/dir3/dir4/target

chmod o+r "$dir"
while [ -n "$dir" ]; do
    chmod o+x "$dir"
    dir=${dir%/*}
done

For the given directory path, this would execute the commands

chmod o+r /dir1/dir2/dir3/dir4/target
chmod o+x /dir1/dir2/dir3/dir4/target
chmod o+x /dir1/dir2/dir3/dir4
chmod o+x /dir1/dir2/dir3
chmod o+x /dir1/dir2
chmod o+x /dir1

The dir variables value is altered in each iteration of the loop by removing the last / and everything after it.

  • thank you. This is what I had thought too. I wanted a confirmation that there are no options that we can use that would tell chmod to do this with a single command – Sree Jul 9 '18 at 10:50
  • @Sreeraj No, unfortunately not for a specific path of only directories like this. With chmod -R you could do it, but you would then change the permissions on a lot more than just those directories. – Kusalananda Jul 9 '18 at 11:29
  • @kusalanda, Yes. Just found that this was feature-requested by someone and was rejected debbugs.gnu.org/cgi/bugreport.cgi?bug=8736 – Sree Jul 9 '18 at 12:31
  • @Sreeraj Huh, why would you need an option to chmod when a perfectly simple loop can do the same thing? – Kusalananda Jul 9 '18 at 12:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.