-2

I am trying to change the permissions for each directory in a path that a user will specify. This will be completed in a bash script. For example:

DIR = /a/b/c/d/e/f/

chmod 777 /a/b/c/d/e/f/

chmod 777 /a/b/c/d/e/

chmod 777 /a/b/c/d/

chmod 777 /a/b/c/

chmod 777 /a/b/

chmod 777 /a/

777 is only an example. Solution should allow for any permissions.

I do not want to change permissions of files within the directories and I can not assume these are the only sub-directories. I only want to change the permissions that are explicitly in the path given by the user.

Jayson
  • 23
  • 4
  • The question is explicitly different from that in that is wants to chmod a folder and all of it's _ancestors_ but none of the siblings of those ancestors. – SpoonMeiser May 24 '18 at 16:57
  • Sounds like a simple loop (while `$f` do `chmod "$f"`, `f=$(basename "$f")`). – Toby Speight May 24 '18 at 17:20
  • Whatever the provlem is that you are trying to solve, **`chmod 777` is almost certainly *wrong and dangerous***. You should revert to a sane permission which doesn't include write access for random strangers to system files. – tripleee May 25 '18 at 05:07
  • Notice also https://stackoverflow.com/questions/2338641/in-a-php-apache-linux-context-why-exactly-is-chmod-777-dangerous and https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment – tripleee May 25 '18 at 05:17
  • @TobySpeight Can you explain how that would solve my question? How would f go from /base/next/ to /base/next/final/ ? – Jayson May 25 '18 at 22:09
  • @jww I was unaware Bash scripting is not considered programming or development. There are other questions on this site regarding bash. – Jayson May 25 '18 at 22:10
  • @tripleee I used 777 as an example. The permissions is insignificant because that can change with the correct solution. – Jayson May 25 '18 at 22:27
  • @Jayson - Bash scripts are on-topic. Perhaps you can point out where the script is. This is malformed so I know you are not running the garbage from the question: *`DIR = /a/b/c/d/e/f/`*. The best I can tell, this is just another question about how to use a command that's already been asked and answered with *"... in a script"* tacked on. – jww May 25 '18 at 22:37
  • @Jayson - the other way around - that takes you from `/a/b/c/d/e/f` to `/a/b/c/d/e`, then to `/a/b/c/d` etc. (And obviously, I meant to write `dirname`, not `basename`). – Toby Speight May 28 '18 at 08:12

2 Answers2

0

The obvious way is to start at the deepest point, and iterate upwards using dirname in a loop:

perms=a+rX
d=a/b/c/d/e/f

while [ "$d" != . ]
do
    chmod $perms "$d"
    d=$(dirname "$d")
done

This is all portable shell, so doesn't depend on Bash - any POSIX-compliant shell will suffice.

I've used relative directory names, as you really don't want to be changing permissions of / on a real system.

You could of course just use a substitution expansion instead of invoking dirname if you want: d=${d%/*}.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
-1

Are you sure you want to set 777 on some directories ?

Anyway, you can achieve this with dirname and recurse on $DIR until the root directory:

DIR=/a/b/c/d/e/f/
while [[ $DIR =~ /.+ ]] ;do
  chmod 777 $DIR
  DIR=$(dirname $DIR)
done
Saïmonn
  • 140
  • 5
  • I like this solution. I see how it works after doing some research on =~ and dirname. 777 was an arbitrary permissions. – Jayson May 25 '18 at 22:34