如何在 Linux 中一步更改文件夹及其所有子文件夹和文件的权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3740152/
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-08-03 23:35:01  来源:igfitidea点击:

How do I change permissions for a folder and all of its subfolders and files in one step in Linux?

linuxpermissionsdirectorychmod

提问by Adam

I would like to change permissions of a folder and all its sub folders and files in one step (command) in Linux.

我想在 Linux 中一步(命令)更改文件夹及其所有子文件夹和文件的权限。

I have already tried the below command but it works only for the mentioned folder:

我已经尝试过以下命令,但它仅适用于提到的文件夹:

chmod 775 /opt/lampp/htdocs

Is there a way to set chmod 755for /opt/lampp/htdocsand all of its content including subfolders and files?

有没有一种方法来设置chmod 755/opt/lampp/htdocs所有内容,包括子文件夹和文件?

Also, in the future, if I create a new folder or file inside htdocs, how can the permissions of that automatically be set to 755?

另外,以后如果我在里面新建一个文件夹或者文件htdocs,那怎么自动设置权限755呢?

I had a look at this link too:

我也看过这个链接:

http://stackoverflow.com/questions/3740187/how-to-set-default-chmod-in-linux-terminal

http://stackoverflow.com/questions/3740187/how-to-set-default-chmod-in-linux-terminal

采纳答案by WombleGoneBad

The other answers are correct, in that chmod -R 755will set these permissions to all files and subfolders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files?

其他答案是正确的,因为chmod -R 755会将这些权限设置为树中的所有文件和子文件夹。但你到底为什么想要?这可能对目录有意义,但为什么要在所有文件上设置执行位?

I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the findcommand. For example:

我怀疑您真正想要做的是将目录设置为 755,然后单独保留文件或将它们设置为 644。为此,您可以使用该find命令。例如:

To change all the directories to 755 (drwxr-xr-x):

要将所有目录更改为 755 ( drwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r--r--):

要将所有文件更改为 644 ( -rw-r--r--):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

回答by Topera

To set to all subfolders (recursively) use -R

要设置为所有子文件夹(递归)使用 -R

chmod 755 /folder -R

And use umask to set the default to new folders/files cd /folder umask 755

并使用 umask 将默认设置为新文件夹/文件 cd /folder umask 755

回答by Steve Robillard

Check the -R option

检查 -R 选项

chmod -R <permissionsettings> <dirname>

chmod -R <permissionsettings> <dirname>

In the future, you can save a lot of time by checking the man page first:

将来,您可以通过先查看手册页来节省大量时间:

man <command name>

So in this case:

所以在这种情况下:

man chmod

回答by Nate Starner

Use:

用:

sudo chmod 755 -R /whatever/your/directory/is

However, be careful with that. It can really hurt you if you change the permissions of the wrong files/folders.

但是,请注意这一点。如果您更改了错误文件/文件夹的权限,这真的会伤害您。

回答by sleepynate

chmod 755 -R /opt/lampp/htdocswill recursively set the permissions. There's no way to set the permissions for files automatically in only this directory that are created after you set the permissions, but you could change your system-wide default file permissions with by setting umask 022.

chmod 755 -R /opt/lampp/htdocs将递归设置权限。无法仅在设置权限后创建的此目录中自动设置文件的权限,但您可以通过设置更改系统范围的默认文件权限umask 022

回答by Pramendra Gupta

The correct recursive command is:

正确的递归命令是:

sudo chmod -R 755 /opt/lampp/htdocs

-R: change every sub folder including the current folder

-R:更改每个子文件夹,包括当前文件夹

回答by wmartin

For Mac OS X 10.7 (Lion), it is:

对于 Mac OS X 10.7 (Lion),它是:

chmod -R 755 /directory

And yes, as all other say, be careful when doing this.

是的,正如所有其他人所说,这样做时要小心。

回答by Pete

If you want to set permissions on all files to a+r, and all directories to a+x, and do that recursively through the complete subdirectory tree, use:

如果要将所有文件的权限设置为a+r,并将所有目录的权限设置为a+x,并通过完整的子目录树递归执行此操作,请使用:

chmod -R a+rX *

The X(that is capital X, not small x!) is ignored for files (unless they are executable for someone already) but is used for directories.

X(即资本X,不小x!)的文件被忽略(除非它们是可执行的人的话),但用于目录。

回答by Iam Zesh

You might want to consider this answer given by nikon superuser and use "one chmod" for all files/folders like this:

您可能需要考虑nik在超级用户上给出的这个答案,并对所有文件/文件夹使用“一个 chmod”,如下所示:

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)

回答by Prabhu

There are two answers of finding files and applying chmodto them. First one is findthe file and apply chmodas it finds (as suggested by @WombleGoneBad).

查找文件和申请文件有两个答案chmod。第一个是find文件并chmod在找到时应用(如@WombleGoneBad 所建议的那样)。

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

Second solution is to generate list of all files with findcommand and supply this list to the chmodcommand (as suggested by @lamgesh).

第二种解决方案是使用find命令生成所有文件的列表并将此列表提供给chmod命令(如@lamgesh 所建议的那样)。

chmod 755 $(find /path/to/base/dir -type d)

Both of these versions work nice as long as the number of files returned by the findcommand is small. The second solution looks great to eye and more readable than the first one. If there are large number of files, the second solution returns error : Argument list too long.

只要find命令返回的文件数量很少,这两个版本都可以很好地工作。第二个解决方案看起来很棒,而且比第一个更易读。如果有大量文件,第二种解决方案返回错误:Argument list too long.

So my suggestion is

所以我的建议是

  1. Use chmod -R 755 /opt/lampp/htdocsif you want to change permissions of all files and directories at once.
  2. Use find /opt/lampp/htdocs -type d -exec chmod 755 {} \;if the number of files you are using is very large. The -type xoption searches for specific type of file only, where d is used for finding directory, f for file and l for link.
  3. Use chmod 755 $(find /path/to/base/dir -type d)otherwise
  4. Better to use the first one in any situation
  1. chmod -R 755 /opt/lampp/htdocs如果要一次更改所有文件和目录的权限,请使用。
  2. 使用find /opt/lampp/htdocs -type d -exec chmod 755 {} \;如果您正在使用的文件的数量是非常大的。该-type x选项仅搜索特定类型的文件,其中 d 用于查找目录,f 用于文件,l 用于链接。
  3. chmod 755 $(find /path/to/base/dir -type d)否则使用
  4. 在任何情况下最好使用第一个