Atul Gupta
Atul Gupta

Reputation: 755

how to change chmod of directories in path

Q How to set chmod of a/ a/b/ a/b/c/ & a/b/c/d/ to 755

Say I have a path a/b/c/d/ to create
I can call mkdir -p a/b/c/d/ and it will create each of the directory in path
Now I want to set chmod of a/ a/b/ a/b/c/ & a/b/c/d/ to 755

Note mkdir -pm 0755 a/b/c/d/ will set chmod to 755 for only the last folder

Upvotes: 4

Views: 3019

Answers (5)

Mexican Jerboa
Mexican Jerboa

Reputation: 1

path=a/b/c/d/
while [[ -n $path ]]; do
    chmod 755 $path
    path=${path%/*}
done

buff's answer doesn't work for me. Here's modification to his answer that does work. Substring removal fixed, and with this approach original path should end with trailing / .

Upvotes: 0

Anugraha Sinha
Anugraha Sinha

Reputation: 670

If you are presently in parent directory of 'a' we could probably do this

chmod 755 a ; find a/ -type d -exec chmod 755 {} \;

Upvotes: 0

buff
buff

Reputation: 2053

In case the directories are already created, you can change the permissions with this bash snippet:

path=a/b/c/d
while [[ -n $path ]]; do
    chmod 755 $path
    path=${path%[^/]*}
done

Upvotes: 3

Tim Potapov
Tim Potapov

Reputation: 443

perldoc -f chmod

chmod LIST

Changes the permissions of a list of files. The first element of the list must be the numeric mode, which should probably be an octal number, and which definitely should not be a string of octal digits: 0644 is okay, but "0644" is not. 

Try something like this:

chmod 0777, "test.txt";

Note

Note chmod is a LIST operator meaning you can pass it a list (or array) like this:

$cnt = chmod 0755, "foo", "bar";

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 754470

Use:

(umask 022; mkdir -p /a/b/c/d)

Setting the umask ensures that the write bits are reset for group and other on any directories the command creates (but has no effect on pre-existing directories, of course). The directories are then created with 755 permissions as desired. The parentheses use a sub-shell so that only the mkdir command is affected by the umask setting. (I use umask 022 by default; I usually don't mind people reading files, but I don't like them changing them without my permission.)

Upvotes: 4

Related Questions