Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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 Stack Overflow question too:

How can I set a default 'chmod' in a Linux terminal?

like image 878
Adam Halasz Avatar asked Sep 18 '10 02:09

Adam Halasz


People also ask

How do I change folder permissions and subfolders?

Changing permissions with chmod To modify the permission flags on existing files and directories, use the chmod command ("change mode"). It can be used for individual files or it can be run recursively with the -R option to change permissions for all of the subdirectories and files within a directory.


2 Answers

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 {} \; 

Some splainin': (thanks @tobbez)

  • chmod 755 {} specifies the command that will be executed by find for each directory
  • chmod 644 {} specifies the command that will be executed by find for each file
  • {} is replaced by the path
  • ; the semicolon tells find that this is the end of the command it's supposed to execute
  • \; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find
like image 57
WombleGoneBad Avatar answered Oct 09 '22 13:10

WombleGoneBad


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 
like image 43
Steve Robillard Avatar answered Oct 09 '22 12:10

Steve Robillard