Joe*_*ams 21
要以非递归方式更改文件或目录条目的权限,请使用以下chmod命令(请参阅man chmod以了解有关其特定选项的更多信息):
chmod +x dir # Set a directory to be listable
chmod +x file # Set a file to be executable
Run Code Online (Sandbox Code Playgroud)
递归更改文件/目录的所有者(影响所有后代):
chown -R username dir # Recursively set user
chown -R username:groupname dir # Recursively set user and group
Run Code Online (Sandbox Code Playgroud)
要更改目录中所有文件的权限位,请递归:
find dir -type f -exec chmod 644 {} ';' # make all files rw-r-r-
Run Code Online (Sandbox Code Playgroud)
要更改所有目录的权限位:
find dir -type d -exec chmod 755 {} ';' # make all directories rwxr-xr-x
Run Code Online (Sandbox Code Playgroud)
如果您可以这样做,那就太好了:
chmod -R 755 dir
Run Code Online (Sandbox Code Playgroud)
然而,这有问题。它对待文件和目录是一样的。上面的命令使所有用户都可以列出和读取目录,但它也使所有文件都可以执行,这通常是您不想做的。
如果我们将其更改为644,我们会遇到另一个问题:
$ chmod -R 644 x2
chmod: cannot access `x2/authors.html': Permission denied
chmod: cannot access `x2/day_of_week.plot': Permission denied
chmod: cannot access `x2/day_of_week.dat': Permission denied
chmod: cannot access `x2/commits_by_year.png': Permission denied
chmod: cannot access `x2/index.html': Permission denied
chmod: cannot access `x2/commits_by_year.plot': Permission denied
chmod: cannot access `x2/commits_by_year_month.plot': Permission denied
chmod: cannot access `x2/files_by_date.png': Permission denied
chmod: cannot access `x2/files.html': Permission denied
...
Run Code Online (Sandbox Code Playgroud)
问题是644去掉了目录列表位,这个副作用阻止了文件树的进一步遍历。您可以使用 来解决此问题sudo,但最终仍会得到对非 root 用户完全无用的目录。
关键是,chmod -R在某些情况下(例如chmod -R g-r)工作得很好,但在您想弄乱-x位的情况下则不然,因为它不分青红皂白地对文件和目录进行操作。
小智 7
chmod有一个-R标志,表示递归更改文件和目录的权限。
您可以使用大写的 'X' 为文件夹做正确的事情: 'X' = "execute/search only if the file is a directory or already have execute permission for some user"
所以,例如: chmod -R ug=rwX,o-rwx 。
将使每个文件的所有者和组可以访问整个树,而其他任何人都无法访问。任何已经可执行的文件在之后仍然可以执行,并且所有目录都将具有用户和组的“x”,而不是其他人。