Changing File Permissions

There are a number of commands that allow the user to change file properties. The most commonly used is the chmod utility, which takes arguments as follows:

chmod [ -fR ] <absolute-mode> file …

chmod [ -fR ] <symbolic-mode-list> file …

The mode to be applied gives the new or modified permissions of the file. For example, if the new permissions for a file should be rwxr--r--, this equates to the value 744. For this case, chmod can be called with an absolute-mode argument as follows:

$ ls -l myfile
-rw-------      1  spate    fcf                        0 Mar  6 10:09 myfile
$ chmod 744 myfile
$ ls -l myfile
-rwxr--r-       1  spate     fcf                       0 Mar 6 10:09 myfile*

To achieve the same result passing a symbolic-mode argument, chmod can be called as follows:

$ ls -l myfile
-rw------        1  spate    fcf                         0 Mar 6 10:09 myfile
$ chmod u+x,a+r myfile
$ ls -l myfile
-rwxr--r-        1  spate    fcf                         0 Mar 6 10:09 myfile*

In symbolic mode, the permissions for user, group, other, or all users can be modified by specifying u, g, o, or a. Permissions may be specified by adding (+), removing (−), or specifying directly (=), For example, another way to achieve the above change is:

$ ls -l myfile
-rw---------     1  spate    fcf                          0 Mar 6 10:09 myfile
$ chmod u=rwx,g=r,o=r myfile
$ ls -l myfile
-rwxr--r-        1  spate    fcf                          0 Mar 6 10:09 myfile*

One last point worthy of mention is the -R argument which can be passed to chmod. With this option, chmod recursively descends through any directory arguments. For example:

$ ls -ld mydir drwxr-xr-x 2 spate ...

Get UNIX Filesystems: Evolution, Design, and Implementation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.