Linux and Unix chown command Change owner Help and examples

What is Linux chown Command

chown is an administrator command is used to change the owner user and/or group of each given file or directory.

chown Command syntax

chown [options] newowner filename/directoryname

chown Command options

  • -c, –changes : like verbose but report only when a change is made.
  • –reference=RFILE :use RFILE’s owner and group rather than specifying OWNER:GROUP values
  • -f, –silent, –quiet:suppress most error messages.
  • –dereference : affect the referenced file of each symbolic link rather than the symbolic link itself. This is the default.
  • -v, –verbose :output a diagnostic for every file processed.
  • –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.
  • -h, –no-dereference:affect symbolic links instead of any referenced file. This is useful only on systems that can change the ownership of a symlink.
  • –preserve-root:Do not operate recursively on ‘/’.
  • –no-preserve-root:do not treat ‘/’ (the root directory) in any special way. This is the default.
  • -R, –recursive:operate on files and directories recursively.
  • -L :traverse every symbolic link to a directory encountered.
  • -H :if a command line argument is a symbolic link to a directory, traverse it.
  • -P :do not traverse any symbolic links. This is the default.
  • –help :display this help and exit.
  • –version :output version information and exit.

chown Command Examples

Changing owner for directories recursively

The ownership of the directories and files contained in them can be changed recursively with -R option.

chown -R exaccount /home/example/

chown Verbose output

The –verbose option shows all the ownership changing. It outputs the diagnostics for each file processed

chown -R –verbose david /home/david/
changed ownership of ‘/home/david/emails’ to david
changed ownership of ‘/home/david/blogs’ to david
changed ownership of ‘/home/david/plugins’ to david

chown Preserve root

To prevent changing the ownership of / directory recursively, –preserve-root is used.

chown -R –preserve-root david /
chown: changing ownership of ‘/proc/1/tasks/1/fd’: Permission denied
chown: changing ownership of ‘/proc/1/tasks/1/fd/status’: Permission denied

chown Silent operation

Lets consider the following scenario .As normal users cannot change the ownership of files owned by others. Then an error is displayed when a that user tries to change the ownership.

chown david /etc/
chown: changing ownership of `/etc/’: Operation not permitted

But if by passing -f or –silent or –quiet option, the error is not displayed.

chown -f david /etc/

 

2,237 total views, 1 views today

Linux and Unix chown command Change owner Help and examples
4.67 (93.33%) 3 votes

5 Comments

  1. I am genuinely thankful to the holder of this web site who has shared this wonderful post at here.

  2. I’m extremely impressed with your writing skills.it is rare to see a great blog like this one nowadays.

  3. Hi there, I enjoy reading the post. good support with great response time, thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*