How to change permissions for a folder and its subfolders/files in one step?

后端 未结 19 1791
情歌与酒
情歌与酒 2020-11-22 13:20

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

I have already tried the below command but it works

相关标签:
19条回答
  • 2020-11-22 14:08

    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)
    
    0 讨论(0)
  • 2020-11-22 14:12

    For anyone still struggling with permission issues, you can back up one folder from the root directory of your project, then add yourself (user) to the directory and give permission to edit everything inside (tested on Mac OS).

    To do that you would run this command (preferred):

    sudo chown -R username:foldername .*
    

    or simply:

    sudo chmod -R 755 foldername
    

    but as mentioned above, need to be careful with the second method.

    0 讨论(0)
  • 2020-11-22 14:13

    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
    0 讨论(0)
  • 2020-11-22 14:15

    To set to all subfolders (recursively) use -R

    chmod 755 /folder -R
    

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

    0 讨论(0)
  • 2020-11-22 14:19

    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?

    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:

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

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

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

    find /opt/lampp/htdocs -type f -exec chmod 644 {} \;
    
    0 讨论(0)
  • 2020-11-22 14:19

    The correct recursive command is:

    sudo chmod -R 755 /opt/lampp/htdocs
    

    -R: change every sub folder including the current folder

    0 讨论(0)
提交回复
热议问题