Chmod
lastmod 2023-03-16created 2014-01-074 edits
The chmod (short for change mode) command is a shell command used in Unix and Unix-like environments. It changes the file system modes of files and directories. The modes include permissions and special modes.
Usage
chmod command options are specified as follows:
$ chmod [options]mode[,mode]file1[file2...]
To see which permissions currently exist, type:
$ ls -lfile
Options
The chmod command has a number of command options that affect its behavior. The most common options are:
-
-R: Changes the modes of files and directories recursively. -
-v: Verbose mode; lists every file being processed.
String modes
For the chmod utility, all permissions and special modes are represented by the mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode consists of three components, which are combined to form a simple string:
$ chmod [references][operator][modes]file1...
References (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified, it defaults to "all"; they are represented by one or more of the following letters:
| Reference | Class | Description |
| | user | The owner of the file |
| | group | Users who are members of the file's group |
| | others | Users who are neither the owner of the file nor members of its group |
| | all | All three of the above, same as "ugo" |
The chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:
| Operator | Description |
| | Adds the specified modes to the specified classes |
| | Removes the specified modes from the specified classes |
| | The specified modes are made the exact modes for the specified classes |
The modes specify which permissions are to be granted to or removed from the specified classes. There are three basic modes that correspond to the basic permissions:
| Mode | Name | Description |
| | read | Reads a file or lists the contents of a directory |
| | write | Writes to a file or directory |
| | excute | Executes a file or recurses a directory tree |
| | special excute | This should be seen not as a permission in itself but as something that can be used instead of |
| | setuid | Details are in the special modes section |
| | sticky | Details are in the special modes section |
The combination of these three components forms a string recognized by the chmod command. Multiple symbolic modes can be separated by commas, so multiple changes can be specified.
Special modes
The chmod command can also change the special modes or additional permissions of files and directories. The symbolic modes use s to indicate the setuid or setgid modes, and t to indicate the sticky mode. These modes can only be applied to the appropriate classes, regardless of whether other classes are specified. Most operating systems support specifying special modes using octal modes, but some do not. On those systems, only symbolic modes can be used.