3.2. Handling File Attributes

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.

3.2.1. chown, chgrp: change the owner and group of one or more files

The syntax of the chown command is as follows:

chown [options] <user[.group]> <file|directory> [file|directory...]

The options include:

  1. -R Recursive; to change the owner of all files and subdirectories in a given directory.

  2. -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.

  3. -c Like -v, but only reports which files have been changed.

Some examples:

  1. chown nobody /shared/book.tex changes the owner of file /shared/book.tex to nobody.

  2. 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.

3.2.2. chmod: changing permissions on files and directories (CHange MODe)

The chmod 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:

The main options are quite similar to those of chown or chgrp:

  1. -R Change permissions recursively.

  2. -v Verbose mode, displays the actions carried out for each file.

  3. -c Like -v but only shows files for which there has been a change of permissions.

Examples:

  1. chmod -R o-w /shared/docs Recursively removes write permission for "others" on all files and subdirectories in the directory /shared/docs/.

  2. 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.

  3. 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.


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2000.
http://www.linux-mandrake.com/