如何一步更改文件夹及其子文件夹/文件的权限 [英] How to change permissions for a folder and its subfolders/files in one step

查看:52
本文介绍了如何一步更改文件夹及其子文件夹/文件的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

chmod 775 /opt/lampp/htdocs

有没有办法为 /opt/lampp/htdocs 及其所有内容(包括子文件夹和文件)设置 chmod 755?

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

另外,以后如果我在htdocs里面新建一个文件夹或文件,如何自动将其权限设置为755?

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?

我也看过这个 Stack Overflow 问题:

I had a look at this Stack Overflow question too:

如何在一个 Linux 终端?

推荐答案

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

The other answers are correct, in that chmod -R 755 will 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?

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

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 find command. For example:

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

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

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

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

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

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

一些说明:(感谢@tobbez)

Some splainin': (thanks @tobbez)

  • chmod 755 {} 指定find 为每个目录执行的命令
  • chmod 644 {} 指定find 为每个文件执行的命令
  • {} 被路径替换
  • ; 分号告诉 find 这是它应该执行的命令的结尾
  • ; 分号是转义,否则会被shell解释find
  • chmod 755 {} specifies the command that will be executed by find for each directory
  • chmod 644 {} specifies the command that will be executed by find for each file
  • {} is replaced by the path
  • ; the semicolon tells find that this is the end of the command it's supposed to execute
  • ; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find

这篇关于如何一步更改文件夹及其子文件夹/文件的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆