Setting directories to 0755 and files to 0644 is straight forward using chmod(1)
. However, sometimes batch processing or in other words the permissions of all files/(sub)folders in a given location should be changed. There are basically two approaches to do so. 1.) using a list of file paths and pipe the path to chmod
. The other option is to use chmod
directly.
find(1)
is commonly used to descend into a directory recursively. To apply chmod
on folders/directories (-type d
) a command to change permissions recursively would look like this:
find /path -type d -exec chmod 0755 {} \;
find /path -type d -print0 | xargs -0 chmod 0755
This command can be used for files (-type f
) accordingly:
find /path -type f print0 | xargs -0 chmod 0644
find /path -type f -exec chmod 0644 {} \;
The second option is to use chmod
directly with its -R
argument which applies changes recursively.
chmod -R a=r-wx,u=wr,a+X /path
a
refers to all which means user, group and other their base permissions are set to 0444
or r-wx
which means ‘read only’.
u
refers to user and it allows the user to read and write when set to 6
or wr
.
If a file path is a directory it is made executable (+X
). In combination this leads to 0755
for directories and 0644
for files.
One more thing. What do these numbers (0755/0644) actually mean?
The first number is set to 0
if not specified otherwise (0755
is the same as 755
). Other meanings of the first numbers are 1
to enable the “sticky bit”, 2
to set group ID on execution and 4
to set user ID on execution.
The more interesting numbers are the remaining three. The first number stands for user
, the second for group
and the third digit stands for others
.
4
allows to read, 2
to write and 1
to execute. These numbers are simply add up and e.g. read, write, execute results in a 7
whereas 6
allows to read and write only and 4
sets permissions to read only.