How do I set chmod for a folder and all of its sub

2019-01-01 01:14发布

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

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?

This works, but only for this folder:

chmod 775 /opt/lampp/htdocs

16条回答
姐姐魅力值爆表
2楼-- · 2019-01-01 01:37

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

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

sudo chmod 755 -R /opt/lampp/htdocs
查看更多
人间绝色
3楼-- · 2019-01-01 01:45

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).

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).

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.

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. 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.
  3. Use chmod 755 $(find /path/to/base/dir -type d) otherwise
  4. Better to use the first one in any situation
查看更多
查无此人
4楼-- · 2019-01-01 01:46

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

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
查看更多
余生无你
5楼-- · 2019-01-01 01:46

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

查看更多
高级女魔头
6楼-- · 2019-01-01 01:47

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:

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.

查看更多
余生请多指教
7楼-- · 2019-01-01 01:48

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.

查看更多
登录 后发表回答