The series of commands shown here are used to change the owner or owner group of a file or its permissions. We looked at the different permissions in chapter Basic UNIX System Concepts.
The syntax of the chown (CHange OWNer) command is as follows:
chown [options] <user[.group]> <file|directory> [file|directory...] |
-R: recursive. To change the owner of all files and subdirectories in a given directory.
-v: verbose mode. Displays all actions performed by chown; reports which files have changed ownership as a result of the command and which files have not been changed.
-c: like -v, but only reports which files have been changed.
chown nobody /shared/book.tex: changes the owner of the /shared/book.tex file to nobody.
chown -Rc queen.music *.mid concerts/: changes the ownership of all files in the current directory whose name ends with .mid and all files and subdirectories in the concerts/ directory to user queen and group music, reporting only files affected by the command.
The chgrp (CHange GRouP) command lets you change the group ownership of a file (or files); its syntax is very similar to that of chown:
chgrp [options] <group> <file|directory> [file|directory...] |
The options for this command are the same as for chown, and it is used in a very similar way. Thus, the command:
chgrp disk /dev/hd* |
changes the ownership of all files in directory /dev/ with names beginning with hd to group disk.
The chmod (CHange MODe) command has a very distinct syntax. The general syntax is:
chmod [options] <change mode> <file|directory> [file|directory...] |
but what distinguishes it is the different forms that the mode change can take. It can be specified in two ways:
in octal. The owner user permissions then correspond to figures with the form <x>00, where <x> corresponds to the permission assigned: 4 for read permission, 2 for write permission and 1 for execute permission. Similarly, the owner group permissions take the form <x>0 and permissions for “others” the form <x>. Then, all you need to do is add together the assigned permissions to get the right mode. Thus, the permissions rwxr-xr-- correspond to 400+200+100 (owner permissions, rwx) +40+10 (group permissions, r-x) +4 (others' permissions, r--) = 754; in this way, the permissions are expressed in absolute terms. This means that previous permissions are unconditionally replaced;
with expressions. Here permissions are expressed by a sequence of expressions separated by commas. Hence an expression takes the following form: [category]<+|-|=><permissions>.
The category may be one or more of:
If no category is specified, changes will apply to all categories. A + sets a permission, a - removes the permission and a = sets the permission. Finally, the permission is one or more of the following:
The main options are quite similar to those of chown and chgrp:
chmod -R o-w /shared/docs: recursively removes write permission for others on all files and subdirectories in the /shared/docs/ directory.
chmod -R og-w,o-x private/: recursively removes write permission for group and others for the whole private/ directory, and removes the execution permission for others.
chmod -c 644 misc/file*: changes permissions of all files in the misc/ directory whose names begin with file to rw-r--r-- (i.e. read permission for everyone and write permission only for the owner), and reports only files affected by this command.