Advertisement
Advertisement


How do I change permissions for a folder and all of its subfolders and files in one step in Linux?


Question

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 only for the mentioned folder:

chmod 775 /opt/lampp/htdocs

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?

I had a look at this link too:

http://stackoverflow.com/questions/3740187/how-to-set-default-chmod-in-linux-terminal

2019/03/21
1
1849
3/21/2019 4:58:48 PM

Accepted Answer

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 {} \;
2018/09/26
3026
9/26/2018 10:48:25 AM

Check the -R option

chmod -R <permissionsettings> <dirname>

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
2013/09/25

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.

2013/11/11

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 -R 755 /opt/lampp/htdocs
2020/03/17

The correct recursive command is:

sudo chmod -R 755 /opt/lampp/htdocs

-R: change every sub folder including the current folder

2020/01/20

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

2010/09/18

Source: https://stackoverflow.com/questions/3740152
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]