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

ID : 425

viewed : 135

Tags : linuxpermissionsdirectorychmodlinux

Top 5 Answer for linux - How to change permissions for a folder and its subfolders/files in one step

vote vote

100

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
vote vote

86

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 
vote vote

71

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.

vote vote

60

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 
vote vote

56

The correct recursive command is:

sudo chmod -R 755 /opt/lampp/htdocs 

-R: change every sub folder including the current folder

Top 3 video Explaining linux - How to change permissions for a folder and its subfolders/files in one step

Related QUESTION?