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

后端 未结 19 2059
情歌与酒
情歌与酒 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: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

提交回复
热议问题