Skip to content
Search

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

u

user

The owner of the file

g

group

Users who are members of the file's group

o

others

Users who are neither the owner of the file nor members of its group

a

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

r

read

Reads a file or lists the contents of a directory

w

write

Writes to a file or directory

x

excute

Executes a file or recurses a directory tree

X

special excute

This should be seen not as a permission in itself but as something that can be used instead of x. It applies execute permission to directories regardless of their current permissions, or applies execute permission to files that already have at least one execute permission set (for the user, group, or others). It is useful only in two cases: when used with '+' and on regular files (such as text files), and when combined with the -R option to give group or others access to a large directory tree without setting execute permission. The second case mostly occurs when "chmod -R a+rx" has just been used, and the same thing can be done by using 'X' instead of "chmod -R a+rx".

s

setuid
/ gid

Details are in the special modes section

t

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.

See also

Favorite site

author Zer0 sitemap