简体   繁体   English

如何更改路径中目录的chmod

[英]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 Q 如何设置a/ a/b/ a/b/c/ & a/b/c/d/ chmod为755

Say I have a path a/b/c/d/ to create假设我有一个路径a/b/c/d/要创建
I can call mkdir -pa/b/c/d/ and it will create each of the directory in path我可以调用mkdir -pa/b/c/d/它将在路径中创建每个目录
Now I want to set chmod of a/ a/b/ a/b/c/ & a/b/c/d/ to 755现在我想将a/ a/b/ a/b/c/ & a/b/c/d/ chmod 设置为755

Note mkdir -pm 0755 a/b/c/d/ will set chmod to 755 for only the last folder注意mkdir -pm 0755 a/b/c/d/将仅将最后一个文件夹的 chmod 设置为755

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).设置umask可确保在命令创建的任何目录上为 group 和 other 重置写入位(当然,对预先存在的目录没有影响)。 The directories are then created with 755 permissions as desired.然后根据需要创建具有755权限的目录。 The parentheses use a sub-shell so that only the mkdir command is affected by the umask setting.括号使用子 shell,因此只有mkdir命令受umask设置的影响。 (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.) (我默认使用umask 022 ;我通常不介意人们阅读文件,但我不喜欢他们在未经我许可的情况下更改它们。)

In case the directories are already created, you can change the permissions with this bash snippet:如果目录已经创建,您可以使用以下 bash 代码段更改权限:

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

perldoc -f chmod perldoc -f chmod

chmod LIST chmod 列表

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.列表的第一个元素必须是数字模式,它可能应该是一个八进制数,绝对不应该是八进制数字的字符串:0644 可以,但“0644”不是。

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:注意 chmod 是一个LIST运算符,这意味着您可以像这样传递一个列表(或数组):

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

如果您目前在“a”的父目录中,我们可能会这样做

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

buff's answer doesn't work for me. buff 的回答对我不起作用。 Here's modification to his answer that does work.这是对他的答案的修改,确实有效。 Substring removal fixed, and with this approach original path should end with trailing / .子串删除已修复,使用这种方法,原始路径应以尾随/结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM