Linux chmod for www sites

How Can We Help?

You are here:
< Back

To recursively give directories read&execute privileges:

find /path/to/base/dir -type d -exec chmod 755 {} +

To recursively give files read privileges:

find /path/to/base/dir -type f -exec chmod 644 {} +

Or, if there are many objects to process:

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)

Or, to reduce chmod spawning:

find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

A common reason for this sort of thing is to set directories to 755 but files to 644. In this case there’s a slightly quicker way than the aboveĀ find example:

chmod -R u+rwX,go+rX,go-w /path

Meaning:

  • -R = recursively;
  • u+rwX = Users can read, write and execute;
  • go+rX = group and others can read and execute;
  • go-w = group and others can’t write

The important thing to note here is that uppercase X acts differently to lowercase x. In manual we can read:

The execute/search bits if the file is a directory or any of the execute/search bits are set in the original (unmodified) mode.

In other words, chmod u+X on a file won’t set the execute bit; and g+X will only set it if it’s already set for the user.

 

https://superuser.com/questions/91935/how-to-chmod-all-directories-except-files-recursively