Change file modes (POSIX)
chmod [-Rv] mode file...
- 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.
- Be verbose; display the operations that are being performed.
- Represents the change to be made to the file mode of each file named (see the Description below).
- The pathname of a file whose file mode bits are to be modified.
The chmod utility lets you change any or all of the file permission mode bits of one or more files. For each file that 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, root.
The mode option can be either a symbolic_mode expression or a nonnegative octal integer.
The symbolic_mode has the following form:
The who part of the symbolic mode is any combination of:
- User, group, and other access
- Group access
- Other access
- User access
The operator is one of:
- 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.
The copy part specifies the unmodified permissions (i.e. before the chmod command has been executed) of one of:
The permissions part is any combination of:
- Read permission
- When executed, set the user ID (if who contains or implies u) and/or group ID (if who contains or implies g)
- Sticky bit
- Write permission
- Execute/search permission if the file is a directory or at least one execute bit is on before any of the file mode bits are modified
- Execute permission
The who specification is optional. When it isn't supplied, all the permissions (user, group and other) are affected, but for + and = operators, only those permissions that aren't set in the file creation mask are set.
The permissions part is also optional. If omitted, it defaults to none (i.e. the command adds no permissions, removes no permissions, or sets the permissions to none, depending on the operator).
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
Add read permission to the user, remove write permission from the user, and set the group permissions to be the same as the other permissions:
chmod u+r,u-w,g=o myfile
In octal mode, permissions are specified with a three-digit octal number. The three digits represent user, group, and other permissions in that order.
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:
For example, 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 number||File mode|
|6000||Setgid 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.
- The utility executed successfully and all requested changes were made.
- An error occurred.
If the mode operand isn't valid, chmod doesn't change the file mode bits of any file.