如何为一个文件夹及其所有子文件夹和文件在Linux Ubuntu终端中设置chmod ?

时间:2022-04-11 07:08:42

Is there a way to set chmod 755 for /opt/lampp/htdocs and 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呢?

This works, but only for this folder:

这是可行的,但仅限于此文件夹:

chmod 775 /opt/lampp/htdocs

16 个解决方案

#1


2139  

The other answers are correct, in that chmod -R 755 will set this as permissions to all files and folders 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 - r755中,将此设置为对树中的所有文件和文件夹的权限。但你究竟为什么要这样做呢?这可能对目录有意义,但是为什么要对所有文件设置执行位呢?

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,或者将文件单独留下,或者将它们设置为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 {} \;

#2


619  

Check the -R option

检查- r选项

chmod -R <permissionsettings> <dirname>

chmod - r < permissionsettings > <目录名>

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

#3


275  

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:

如果您希望将所有文件的权限设置为+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 !)被忽略为文件(除非它们已经是可执行的),但用于目录。

#4


97  

You can use -R with chmod for recursive traversal of all files and subfolders.

可以使用-R与chmod进行所有文件和子文件夹的递归遍历。

You might need sudo as it depends on LAMP being installed by the current user or another one:

您可能需要sudo,因为它依赖于当前用户或另一个用户安装的LAMP:

sudo chmod 755 -R /opt/lampp/htdocs

#5


64  

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 /文件夹umask 755。

#6


44  

chmod 755 -R /opt/lampp/htdocs will 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来更改系统范围的默认文件权限。

#7


33  

The correct recursive command is:

正确的递归命令是:

sudo chmod 755 -R /opt/lampp/htdocs

-R: change every sub folder including the current folder

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

#8


20  

You might want to consider this answer given by nik on 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)

#9


18  

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.

但是,要小心。如果您更改了错误的文件/文件夹的权限,就会对您造成很大的伤害。

#10


18  

Here's another way to set directories to 775 and files to 664.

这是另一种设置目录到775和文件到664的方法。

find /opt/lampp/htdocs \
\( -type f -exec chmod ug+rw,o+r {} \; \) , \
\( -type d -exec chmod ug+rwxs,o+rx {} \; \)

It may look long, but it's pretty cool for three reasons:

它看起来很长,但它很酷有三个原因:

  1. Scans through the file system only once rather than twice.
  2. 通过文件系统扫描一次而不是两次。
  3. Provides better control over how files are handled vs. how directories are handled. This is useful when working with special modes such as the sticky bit, which you probably want to apply to directories but not files.
  4. 对如何处理文件和如何处理目录提供更好的控制。这在使用特殊模式(如粘性位)时非常有用,您可能希望将其应用于目录而不是文件。
  5. Uses a technique straight out of the man pages (see below).
  6. 使用一种直接从手册页(见下文)的技术。

Note that I have not confirmed the performance difference (if any) between this solution and that of simply using two find commands (as in Peter Mortensen's solution). However, seeing a similar example in the manual is encouraging.

注意,我还没有确定这个解决方案与简单使用两个find命令(如Peter Mortensen的解决方案)之间的性能差异(如果有的话)。然而,在手册中看到类似的例子是令人鼓舞的。

Example from man find page:

例子来自人类发现页:

find / \
\( -perm -4000 -fprintf /root/suid.txt %#m %u %p\n \) , \
\( -size +100M -fprintf /root/big.txt %-10s %p\n \)

Traverse the filesystem just once, listing setuid files and  direc‐
tories into /root/suid.txt and large files into /root/big.txt.

Cheers

干杯

#11


11  

chmod -R 755 directory_name works, but how would you keep new files to 755 also? The file's permissions becomes the default permission.

chmod - r755 directory_name工作,但是如何将新文件保存到755 ?文件的权限成为默认权限。

#12


9  

You want to make sure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want

您希望确保适当的文件和目录都是chmod-ed/权限。您想要的所有目录。

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

And for all the images, JavaScript, CSS, HTML...well, you shouldn't execute them. So use

对于所有的图像,JavaScript, CSS, HTML…你不应该执行它们。所以使用

chmod 644 img/* js/* html/*

But for all the logic code (for instance PHP code), you should set permissions such that the user can't see that code:

但是对于所有的逻辑代码(例如PHP代码),您应该设置权限,这样用户就看不到这些代码:

chmod 600 file

#13


7  

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.

是的,就像所有人说的那样,在做这件事的时候要小心。

#14


5  

I think Adam was asking how to change umask value for all processes that tying to operate on /opt/lampp/htdocs directory.

我认为Adam正在询问如何改变所有与操作/opt/lampp/htdocs目录有关的进程的umask值。

The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control the default file permission for new files.

用户文件创建模式掩码(umask)用于确定新创建的文件的文件权限。它可以用来控制新文件的默认文件权限。

so if you will use some kind of ftp program to upload files into /opt/lampp/htdocs you need to configure your ftp server to use umask you want.

因此,如果你要使用某种ftp程序来将文件上传到/opt/lampp/htdocs,你需要配置你的ftp服务器来使用你想要的umask。

If files / directories be created for example by php, you need to modify php code

如果用php创建文件/目录,则需要修改php代码。

<?php
umask(0022);
// other code
?>

if you will create new files / folders from your bash session, you can set umask value in your shell profile ~/.bashrc Or you can set up umask in /etc/bashrc or /etc/profile file for all users. add the following to file: umask 022

如果您将从您的bash会话创建新的文件/文件夹,您可以在您的shell概要文件中设置umask值~/。bashrc或您可以在/etc/bashrc或/etc/profile文件中为所有用户设置umask。添加以下文件:umask 022。

Sample umask Values and File Creation Permissions
If umask value set to   User permission     Group permission     Others permission
000                         all              all                   all
007                         all              all                   none
027                         all          read / execute            none

And to change permissions for already created files you can use find. Hope this helps.

并更改已经创建的文件的权限,您可以使用find。希望这个有帮助。

#15


4  

There are two answers of finding files and applying chmod to them. First one is find the file and apply chmod as it finds (as suggested by @WombleGoneBad).

找到文件并将chmod应用于它们有两个答案。第一个是找到文件并应用chmod查找(如@WombleGoneBad所建议的)。

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

Second solution is to generate list of all files with find command and supply this list to the chmod command (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 find command 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命令返回的文件数量很小,这两个版本都可以工作得很好。第二个解决方案看起来比第一个解决方案更有可读性。如果有大量的文件,第二个解决方案返回错误:参数列表太长。

So my suggestion is

所以我的建议是

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

#16


3  

It's very simple.

很简单。

In Terminal go to file manager. example: sudo nemo. Go /opt/ then click Properties → Permission. and then Other. Finally, change to create and delete and file acess to read and write and click on button apply... And work.

在终端到文件管理器。例子:sudo尼莫。Go /opt/然后点击属性。然后其他。最后,更改创建和删除文件,并将acess用于读取和写入,点击按钮应用…和工作。

#1


2139  

The other answers are correct, in that chmod -R 755 will set this as permissions to all files and folders 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 - r755中,将此设置为对树中的所有文件和文件夹的权限。但你究竟为什么要这样做呢?这可能对目录有意义,但是为什么要对所有文件设置执行位呢?

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,或者将文件单独留下,或者将它们设置为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 {} \;

#2


619  

Check the -R option

检查- r选项

chmod -R <permissionsettings> <dirname>

chmod - r < permissionsettings > <目录名>

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

#3


275  

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:

如果您希望将所有文件的权限设置为+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 !)被忽略为文件(除非它们已经是可执行的),但用于目录。

#4


97  

You can use -R with chmod for recursive traversal of all files and subfolders.

可以使用-R与chmod进行所有文件和子文件夹的递归遍历。

You might need sudo as it depends on LAMP being installed by the current user or another one:

您可能需要sudo,因为它依赖于当前用户或另一个用户安装的LAMP:

sudo chmod 755 -R /opt/lampp/htdocs

#5


64  

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 /文件夹umask 755。

#6


44  

chmod 755 -R /opt/lampp/htdocs will 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来更改系统范围的默认文件权限。

#7


33  

The correct recursive command is:

正确的递归命令是:

sudo chmod 755 -R /opt/lampp/htdocs

-R: change every sub folder including the current folder

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

#8


20  

You might want to consider this answer given by nik on 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)

#9


18  

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.

但是,要小心。如果您更改了错误的文件/文件夹的权限,就会对您造成很大的伤害。

#10


18  

Here's another way to set directories to 775 and files to 664.

这是另一种设置目录到775和文件到664的方法。

find /opt/lampp/htdocs \
\( -type f -exec chmod ug+rw,o+r {} \; \) , \
\( -type d -exec chmod ug+rwxs,o+rx {} \; \)

It may look long, but it's pretty cool for three reasons:

它看起来很长,但它很酷有三个原因:

  1. Scans through the file system only once rather than twice.
  2. 通过文件系统扫描一次而不是两次。
  3. Provides better control over how files are handled vs. how directories are handled. This is useful when working with special modes such as the sticky bit, which you probably want to apply to directories but not files.
  4. 对如何处理文件和如何处理目录提供更好的控制。这在使用特殊模式(如粘性位)时非常有用,您可能希望将其应用于目录而不是文件。
  5. Uses a technique straight out of the man pages (see below).
  6. 使用一种直接从手册页(见下文)的技术。

Note that I have not confirmed the performance difference (if any) between this solution and that of simply using two find commands (as in Peter Mortensen's solution). However, seeing a similar example in the manual is encouraging.

注意,我还没有确定这个解决方案与简单使用两个find命令(如Peter Mortensen的解决方案)之间的性能差异(如果有的话)。然而,在手册中看到类似的例子是令人鼓舞的。

Example from man find page:

例子来自人类发现页:

find / \
\( -perm -4000 -fprintf /root/suid.txt %#m %u %p\n \) , \
\( -size +100M -fprintf /root/big.txt %-10s %p\n \)

Traverse the filesystem just once, listing setuid files and  direc‐
tories into /root/suid.txt and large files into /root/big.txt.

Cheers

干杯

#11


11  

chmod -R 755 directory_name works, but how would you keep new files to 755 also? The file's permissions becomes the default permission.

chmod - r755 directory_name工作,但是如何将新文件保存到755 ?文件的权限成为默认权限。

#12


9  

You want to make sure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want

您希望确保适当的文件和目录都是chmod-ed/权限。您想要的所有目录。

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

And for all the images, JavaScript, CSS, HTML...well, you shouldn't execute them. So use

对于所有的图像,JavaScript, CSS, HTML…你不应该执行它们。所以使用

chmod 644 img/* js/* html/*

But for all the logic code (for instance PHP code), you should set permissions such that the user can't see that code:

但是对于所有的逻辑代码(例如PHP代码),您应该设置权限,这样用户就看不到这些代码:

chmod 600 file

#13


7  

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.

是的,就像所有人说的那样,在做这件事的时候要小心。

#14


5  

I think Adam was asking how to change umask value for all processes that tying to operate on /opt/lampp/htdocs directory.

我认为Adam正在询问如何改变所有与操作/opt/lampp/htdocs目录有关的进程的umask值。

The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control the default file permission for new files.

用户文件创建模式掩码(umask)用于确定新创建的文件的文件权限。它可以用来控制新文件的默认文件权限。

so if you will use some kind of ftp program to upload files into /opt/lampp/htdocs you need to configure your ftp server to use umask you want.

因此,如果你要使用某种ftp程序来将文件上传到/opt/lampp/htdocs,你需要配置你的ftp服务器来使用你想要的umask。

If files / directories be created for example by php, you need to modify php code

如果用php创建文件/目录,则需要修改php代码。

<?php
umask(0022);
// other code
?>

if you will create new files / folders from your bash session, you can set umask value in your shell profile ~/.bashrc Or you can set up umask in /etc/bashrc or /etc/profile file for all users. add the following to file: umask 022

如果您将从您的bash会话创建新的文件/文件夹,您可以在您的shell概要文件中设置umask值~/。bashrc或您可以在/etc/bashrc或/etc/profile文件中为所有用户设置umask。添加以下文件:umask 022。

Sample umask Values and File Creation Permissions
If umask value set to   User permission     Group permission     Others permission
000                         all              all                   all
007                         all              all                   none
027                         all          read / execute            none

And to change permissions for already created files you can use find. Hope this helps.

并更改已经创建的文件的权限,您可以使用find。希望这个有帮助。

#15


4  

There are two answers of finding files and applying chmod to them. First one is find the file and apply chmod as it finds (as suggested by @WombleGoneBad).

找到文件并将chmod应用于它们有两个答案。第一个是找到文件并应用chmod查找(如@WombleGoneBad所建议的)。

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

Second solution is to generate list of all files with find command and supply this list to the chmod command (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 find command 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命令返回的文件数量很小,这两个版本都可以工作得很好。第二个解决方案看起来比第一个解决方案更有可读性。如果有大量的文件,第二个解决方案返回错误:参数列表太长。

So my suggestion is

所以我的建议是

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

#16


3  

It's very simple.

很简单。

In Terminal go to file manager. example: sudo nemo. Go /opt/ then click Properties → Permission. and then Other. Finally, change to create and delete and file acess to read and write and click on button apply... And work.

在终端到文件管理器。例子:sudo尼莫。Go /opt/然后点击属性。然后其他。最后,更改创建和删除文件,并将acess用于读取和写入,点击按钮应用…和工作。