chown - Unix, Linux Command


Advertisements

Previous Page
Next Page  
 

NAME

chown - change file owner and group

SYNOPSIS

chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

DESCRIPTION

chown changes the user and/or group ownership of each given file. If only an owner (a user name or numeric user ID) is given, that user is made the owner of each given file, and the files' group is not changed. If the owner is followed by a colon and a group name (or numeric group ID), with no spaces between them, the group ownership of the files is changed as well. If a colon but no group name follows the user name, that user is made the owner of the files and the group of the files is changed to that user's login group. If the colon and group are given, but the owner is omitted, only the group of the files is changed; in this case, chown performs the same function as chgrp. If only a colon is given, or if the entire operand is empty, neither the owner nor the group is changed.

OPTIONS

TAG DESCRIPTION
-c, --changes like verbose but report only when a change is made
--dereference affect the referent of each symbolic link (this is the default), rather than the symbolic link itself
-h, --no-dereference affect each symbolic link instead of any referenced file (useful only on systems that can change the ownership of a symlink)

 

--from=CURRENT_OWNER:CURRENT_GROUP change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute.
--no-preserve-root do not treat '/' specially (the default)
-preserve-root do not treat '/' specially (the default)
-f, --silent, --quiet suppress most error messages
--reference=RFILE use RFILE's owner and group rather than specifying OWNER:GROUP values
-R, --recursive operate on files and directories recursively
-v, --verbose output a diagnostic for every file processed

EXAMPLES

EXAMPLE-1:

Change file ownership to john user and list the permissions, run:

# chown john demo.txt

output: # ls -l demo.txt -rw-r--r-- 1 john root 0 Aug 31 05:48 demo.txt

EXAMPLE-2:

Change only the group of file. To do so, the colon and following GROUP-name ftp are given, but the owner is omitted, only the group of the files is changed:

# chown :ftp demo.txt

output: # ls -l demo.txt -rw-r--r-- 1 john ftp 0 Aug 31 05:48 demo.txt

EXAMPLE-3:

The owner is set to john followed by a colon and a group onwership is also set to john group, run:

# chown john:john demo.txt

output: # ls -l demo.txt -rw-r--r-- 1 john john 0 Aug 31 05:48 demo.txt

EXAMPLE-4:

Copy the owner/group settings from one file to another

# ls -l file

-rwxr-xr-x 1 himanshu family 8968 2012-04-09 07:10 file

# ls -l tmpfile

-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

# chown --reference=file tmpfile

# ls -l tmpfile

-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile


EXAMPLE-5:

To change owner only if a file is owned by a particular user

Using chown “–from” flag, you can change the owner of a file, only if that file is already owned by a particular owner.

# ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

# chown --from=guest himanshu tmpfile

# ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile

# chown --from=root himanshu tmpfile

# ls -l tmpfile
-rw-r--r-- 1 himanshu friends 0 2012-05-22 20:03 tmpfile

EXAMPLE-6:

To change the owner/group of the files by traveling the directories recursively

This is made possible by the ‘-R’ option.

# ls -l linux/linuxKernel
-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/linuxKernel

# ls -l linux/ubuntu/ub10
-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/ubuntu/ub10

# ls -l linux/redhat/rh7
-rw-r--r-- 1 root friends 0 2012-05-22 21:52 linux/redhat/rh7

# chown -R himanshu:family linux/

# ls -l linux/redhat/rh7
-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/redhat/rh7

# ls -l linux/ubuntu/ub10
-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/ubuntu/ub10

# ls -l linux/linuxKernel
-rw-r--r-- 1 himanshu family 0 2012-05-22 21:52 linux/linuxKernel

EXAMPLE-7:

List all the changes made by the chown command

Use the verbose option -v, which will display whether the ownership of the file was changed or retained as shown below.

# chown -v -R guest:friends linux

output:
changed ownership of `linux/redhat/rh7' to guest:friends changed ownership of `linux/redhat' retained to guest:friends ownership of `linux/redhat_sym' retained as guest:friends ownership of `linux/ubuntu_sym' retained as guest:friends changed ownership of `linux/linuxKernel' to guest:friends changed ownership of `linux/ubuntu/ub10' to guest:friends ownership of `linux/ubuntu' retained as guest:friends ownership of `linux' retained as guest:friends
 

 


Previous Page
Next Page