How to change permissions for a folder and all its sub folders and files in one step in Linux/Ubuntu?

· · 2994 views

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

I have already tried the following command, but it only works 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 contents, including subfolders and files?

Also, in the future, if I create a new folder or file inside htdocs, how would it be possible to automatically set permissions for that 755?

I looked at this link too:

0
2 Answers

I suspect you actually want to set the directories to 755 and either leave the files alone or set them to 644. The command can be used to do this find. For instance:

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

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

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

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

0

While commands proposed by @olivia will work, they can get really slow. Keep in mind that when using find with -exec the chmod command will be executed for each found entry separately. It can be done much faster though:

find /opt/lampp/htdocs -type d -print0 | xargs -0 chmod 755 
find /opt/lampp/htdocs -type f -print0 | xargs -0 chmod 644
0

Please login or create new account to participate in this conversation.