Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a string of directories and i want to change the permission on all of these directories to 775 (rwxrwxr-x) with a command like chmod.

The problem is that the string is editable so it can be different from user to user, so chmod -R doesn't work because one user may have


while another user has


Thanks for the help :)

share|improve this question
Is there a criteria to enumerate these directories since they will be different for all users? If you can enumerate them with a find command, it would be pretty easy to do. If the change is random and there is no way of listing them all programatically, you'd have to do it on a per user basis using terdon's suggestions. Also, please note if this should be affecting just the folder or all the files inside as well. – MaQleod Mar 19 '13 at 18:55
Is there something that you know? I mean that is the same for all users? Does a/b/c also change? Please give us an actual example and specify which parts of the string are always the same. – terdon Mar 19 '13 at 19:07
yes a/b/c changes as well. – Gimbergsson Mar 19 '13 at 19:09
So, if the string is /a/b/c/d, you want to change the permissions of /a, /a/b, /a/b/c and /a/b/c/d? – Dennis Mar 19 '13 at 19:10
Let me get this straight, you want to be able to change the permissions of an arbitrary set of directories (without affecting their files) automatically? How would the hypothetical command know which directories to change? Will you be giving a different string each time? Please post a few specific examples so we can understand what you are trying to do. – terdon Mar 19 '13 at 19:21

If the variable string contains the path /a/b/c/d, the following instructions will change the permissions of /a, /a/b, /a/b/c and /a/b/c/d:

    unset substring

    for dir in $string; do
        unset IFS
        test "$substring" != "/" && chmod 755 "$substring"

How it works

  • The parentheses execute everything in a subshell, so changing the variables IFS and substring won't have any effect outside the subshell.

  • IFS=/ sets the internal file separator to slash, so the for loop will split $string at the slashes.

  • for dir in $string; do ... done will execute ... once for every component in $string.

    In our example, $dir will take the values , a, b, c and d.

  • unset IFS changes the internal file separator back to default, since it will affect slashes in the directory paths.

  • substring="$substring$dir/" appends the current value of $dir to $substring.

    In our example, $susbtring will take the values /, /a/, /a/b/, /a/b/c/ and /a/b/c/d/.

  • test "$substring" != "/" && chmod 755 "$substring" checks if the current value of $susbtring is different from /.

    If it is, it sets the desired permissions.

share|improve this answer
Very nice, +1. Especially for using quotes so it works with dir names that have spaces. – terdon Mar 19 '13 at 21:45

Your question is not very clear. Are these directories subdirectories of each other? Anyway, here are some choices:

  1. Change permissions on dir1 and all its subdirectories and files

    chmod -R 775 dir1
  2. Change permissions on dir1 and specific subdirectories

    chmod  775 dir1 dir1/dir2 /dir1/dir2/dir3
  3. Change permissions on all subdirectories of dir1

    find dir1 -type d -exec chmod 775 {}\;
share|improve this answer
I think your chmod -R would change perms on files too? I think `find -type d -exec chmod 775 {} \;' or equiv is what you want. – Rich Homolka Mar 19 '13 at 18:46
that doesn't work because you would have to know the first directory and that could be anything. – Gimbergsson Mar 19 '13 at 18:52
Why don't you know the first directory? Why do you need to make a very generic thing? What's the use case? – cpast Mar 19 '13 at 18:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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