From LQWiki
Jump to navigation Jump to search

chown is the command to change ownership of a file or directory.

In Linux and other Unix-like operating systems, each user on the system has control over what other users can do to his or her files, and every file on the system belongs to some user. The ownership of a file has a number of consequences and, in some cases, it may be useful to change the ownership of a file.

Changing ownership of a file to yourself would allow you to modify its permissions, and effectively lock another user out of reading or writing it. For this reason, unprivileged users are not allowed to change the ownership of files; only the superuser is allowed to do this.

Using chown

Chown is used as follows:

chown owner file

For example, if you wanted to "give" the file picture.jpg to the user john, you could use

chown john picture.jpg

Instead of just the owner, the group can be changed as well, or both the user and group. Use a colon to separate owner from group (with no space). Any user can change the group ownership of files that he or she owns using the chgrp command but only the superuser can use chown.

For example,

chown john:family picture.jpg

changes the owner of picture.jpg to john, and the group to family

Changing ownership of a tree

If you want to transfer ownership of a whole section of a tree, you can use:

chown -R /path/to/subtree

-R means "recursive."

For instance, if you want to change ownership of the immediate subtree to root, you can write:

chown -R root.root .

It is tempting to use "*" to refer to all files. However, this will not change ownership of hidden files whose names begin with a dot. Nor can this be done by using ".*", because this will match against "..", and change ownership of all files in the parent directory as well! Note that the argument when using -R is a directory, not individually named files.

Provided by

Most (all?) Linux distributions incorporate this from the GNU Coreutils: man page

Related Commands

  • chgrp- Changes group ownership of a file
  • chmod - Changes the user/group permissions of a file/directory
  • ls - Lists files
  • touch - update timestamps