bash 是否有 shell 命令递归地授予目录和文件的权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/628277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:44:02  来源:igfitidea点击:

Is there a shell command to recursively give permission to directories and files?

bashshellcommand-line

提问by Abs

Can someone please give me a recursive command that will go through a directory and make all normal files permission 644 and all sub directories 755?

有人可以给我一个递归命令,该命令将遍历一个目录并使所有普通文件的权限为 644,所有子目录的权限为 755?

I am really getting tired of doing this manually every time I have to install something on my host. I don't know enough BASH (Shell?) command to do this.

每次必须在主机上安装某些东西时,我真的厌倦了手动执行此操作。我不知道足够的 BASH(Shell?)命令来执行此操作。

回答by ypnos

There is X option for that.

有 X 选项。

chmod a+X * -R

This will give execute bit only to directories, not files. To set 644, 755, respectively with one command, use:

这只会为目录而不是文件提供执行位。用一个命令分别设置 644、755,使用:

chmod a=rX,u+w <files/dirs> -R

回答by Kaarel

First line changes file permissions, and the second changes directory permissions in the active directory and its subdirectories.

第一行更改文件权限,第二行更改活动目录及其子目录中的目录权限。

find . -type f -print0 | xargs -0 chmod 644
find . -type d -print0 | xargs -0 chmod 755

回答by sth

Using symbolic mode names instead of raw numeric permissions:

使用符号模式名称而不是原始数字权限:

chmod -R u=rwX,go=rX somedir

The Xpermission flag only sets directories or already executable files as executable, the -Rflag means "recursive" and applies the permissions to all the contents of the somedir.

X许可标志仅设置目录或已可执行文件为可执行,该-R标志表示“递归”,并应用权限的所有内容somedir

回答by J?rg W Mittag

No, there is no command to recursively change the permissions. If there were such a command, it would violate the Unix mantra: Do One Thing And Do It Well.

不,没有递归更改权限的命令。如果有这样的命令,那就违背了 Unix 的口头禅:Do One Thing And Do It Well。

However, there are twocommands: one for recursing (find), and one for changing permissions (chmod).

但是,有两个命令:一个用于递归 ( find),另一个用于更改权限 ( chmod)。

So, the magic command line is:

所以,神奇的命令行是:

find . -type d -exec chmod 0755 '{}' + -or -type f -exec chmod 0644 '{}' +