The chmod command (abbreviated from change mode) is a shell command in Unix-like environments.
When executed, the command can change file system modes of files and directories. The modes include permissions and special modes.
History
A
chmod command first appeared in
AT&T UNIX version 1, and is still used today on UNIX-like machines.
Usage
The
chmod command options are specified like this:
$ chmod * mode file1 ...
To view what the permissions currently are type:
$ ls -l
Options
The
chmod command has a number of command line options that affect its behavior. The most common options are:
- -R: Changes the modes of directories and files recursively
- -v: Verbose mode; lists all files as they are being processed
String modes
- See also: Symbolic notation of file system permissions
To the
chmod utility, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text:
- classes operator modes
The classes are used to distinguish the users to whom the permissions apply. They are represented by one or more of the following letters:
- u (user), which represents the permissions granted to the owner of the file,
- g (group), which represents the users who are members of the file's group, and
- o (others), which represents any users who are not the owner of the file or members of the group, or
- a (all), which represents all three of the above.
The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions:
- r (read), which grants the ability to read the contents of a file or list a directory's contents,
- w (write), which grants the ability to modify the contents of a file or modify contents in a directory,
- x (execute), which grants the ability to execute a file or recurse a directory tree, and
- X (special execute), which grants the ability to execute similar to execute, but only applies to directories or files that already have the execute permission set so as to allow users to allow traversal of large directory trees.
Finally, the chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:
- +, which adds the specified modes to the specified classes,
- -, which removes the specified modes from the specified classes, and
- =, which means that the modes specified are to be made the exact modes for the specified classes.
The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas.
For example, the following command would be used to add the read and write permissions to the user and group classes of a file or directory named sample:
$ chmod ug+rw sample
Octal numbers
- See also: Octal notation of file system permissions
The chmod command also accepts three- and four-digit octal numbers representing modes. See the article mentioned above for more information about octal notation.
An example of the chmod command using a four-digit octal number to set the modes of a file or directory named sample:
$ chmod 0664 sample
Special modes
- See also: Additional file system permissions
The chmod command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use s to represent the setuid and setgid modes, and t to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified.
Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used.
Examples
- chmod +r file – read is added for all
- chmod -x file – execute permission is removed for all
- chmod u=rw,go= file – read and write is set for the owner, all permissions are cleared for the group and others
- chmod +rw file – change the permissions of the file file to read and write for all.
- chmod -R u+w,go-w docs/ – change the permissions of the directory docs and all its contents to add write access for the user, and deny write access for everybody else.
- chmod 666 file – read and write access to the owner, the group, and all others.
- chmod 0755 file - Equivalent to u=rwx,go=rx. The 0 specifies no special modes.
- chmod 4755 file - The 4 specifies set user ID.
- find path/ -type d -exec chmod a-x {} \; - sets a-x for all directories in tree starting from path/ (use '-type f' to match files only).
- chmod -R u+rwX,g-rwx,o-rwx – Set a directory tree to rwx for owner directories, rw for for owner files, --- for group and all.
See also
External links
Unix software
Chmod | Chmod | Chmod | Chmod | Chmod | Chmod | Chmod