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 the chapter <Basic Unix concepts of the User Guide.
The syntax of the chown command is as follows:
chown [options] <user[.group]> <file|directory> [file|directory...] |
The options include:
-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 owner as a result of the command and which files have not been changed.
-c Like -v, but only reports which files have been changed.
Some examples:
chown nobody /shared/book.tex changes the owner of file /shared/book.tex to nobody.
chown -Rc darth.music *.mid concerts/ attributes all files in the current directory ending with .mid and all files and subdirectories in the directory concerts/ to user darth and to group music, reporting only files affected by the command.
The chgrp 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 all files in the directory /dev/ with names beginning with hd to the group disk.
The chmod command has a very distinct syntax. The general syntax is:
chmod [options] <change mode> <file|directory> [file|directory...] |
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 command number. 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, with an expression taking the form [category]<+|-><permissions>. The category may be one or more of u (User, permissions for owner), g (Group, permissions for owner group) or o (Others, permissions for "others"). If no category is specified the changes apply to all categories. A + sets a permission, a - removes the permission. Finally, the permission is one or more of r (Read), w (Write) or x (eXecute).
The main options are quite similar to those of chown or chgrp:
-R Change permissions recursively.
-v Verbose mode, displays the actions carried out for each file.
-c Like -v but only shows files for which there has been a change of permissions.
Examples:
chmod -R o-w /shared/docs Recursively removes write permission for "others" on all files and subdirectories in the directory /shared/docs/.
chmod -R og-w,o-x private/ Recursively removes write permission for the group and others for the whole directory private/, and removes the execution permission for others.
chmod -c 644 miscellaneous/file* changes permissions of all files in directory miscellaneous/ with names beginning with file to rw-r--r-- (i.e. read permission for everyone and write permission only for the owner), and reports only files where a change has been made.