chmod

change file modes (POSIX)

Syntax:

chmod [-R] mode file...

Options:

-R
Recursively change file modes. For each file that names a directory, chmod changes the file mode bits of the directory and all files in the file hierarchy below it.
mode
Represents the change to be made to the file mode of each file named (see the Description below).
file
The pathname of a file whose file mode bits are to be modified.

Description:

The chmod utility lets you change any or all of the file permission mode bits of one or more files. For each file you name, chmod changes the file permission mode bits according to the mode operand.

To change a file's permission mode bits, the user of chmod must be either the owner of the file or the superuser.

The mode option can be either a symbolic_mode expression or a non-negative octal integer.

Symbolic Modes

The symbolic_mode has the following form:

The options of the symbolic form are:

OptionDescription
auser, group, and other access
ggroup access
oother access
uuser access
+add specified permissions to the group, other, or user category of the specified files
-remove specified permissions from the group, other, or user category of the specified files
=set the specified permissions for the group, other, or user category of the specified files
rread permission
sset userid or groupid when executed
wwrite permission
xexecute permission

The agou specification is optional. When it is not supplied, all the permissions (user, group and other) will be affected, but for + and = operators only those permissions not set in the file creation mask (see umask) will be set.

Some examples of symbolic modes:

Make myfile executable by all:

chmod a+x myfile

Remove read permission for group and others:

chmod og-r myfile

Perform both the above operations, in the order given, on three files: myfile, file2, and zzz:

chmod a+x,og-r myfile file2 zzz

Octal Modes

Each permission may be specified with an octal number: read = 4; write = 2; execute = 1; no permission = 0. The octal equivalents are derived by adding the numbers associated with the four basic permissions. The following table illustrates their use:

Octal numberSymbolicPermission
0---none
1--xexecute
2-w-write
3-wxwrite/execute
4r--read
5r-xread/execute
6rw-read/write
7rwxread/write/execute

In the octal form, the three octal digits represent user, group, and other permissions in that order.

Example of octal modes: Give the user read/write/execute (octal 7 = rwx), group read/execute (octal 5 = r-x), and other read only (octal 4 = r--) for the file myfile:

chmod 754 myfile

Setgid and setuid

The following table shows how the setgid and setuid file modes are represented in octal:

Octal numberFile mode
2000setgid
4000setuid
6000setgid and setuid

You can combine these file modes with the permission modes described above. For example:

chmod 4666 testfile

In this case, setuid is set, and the user, group, and other get read/write access.

Exit status:

0
The utility executed successfully and all requested changes were made.
>0
An error occurred.

Consequences of Errors:

If the mode operand isn't valid, chmod doesn't change the file mode bits of any file.

See also: