Overview

Linux CHOWN command is used to change the owner or/and group of any file or directory. Linux Chown command has the capability to define ownership of any file or directory. Some interesting usage of Linux CHOWN command is 

  • If only owner is provided with CHOWN command than only owner of the file is changes without changing the group of the file.
  • To change both owner as well as group of the file, we need to provide owner and group separated by colon.
  • If only colon group name is provided than it will only change the group of the file and will act like CHGRP command.
  • If only owner name followed by colon is present, than the owner will be updated with the user and group will be updated by group's name.
  • If only colon is present than not owner will get change and not the group of that file.

Also have a look at CHMOD, CHGRP commands in our earlier posts.

Syntax

chown [OPTION]... [OWNER][:[GROUP]] FILE...

Options

The following options modify how a hierarchy is traversed when the -R option is also specified. If more than one is specified, only the final one takes effect.

-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links 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 fail to operate recursively on '/'
--reference=RFILE use RFILE's owner and group rather than specifying OWNER:GROUP values
-R, --recursive operate on files and directories recursively
-H if a command line argument is a symbolic link to a directory, traverse it
-L traverse every symbolic link to a directory encountered
-P do not traverse any symbolic links (default)
--help display this help and exit
--version output version information and exit

Examples

How to change the owner of a file?

To change the owner of a file the CHOWN command is

CHOWN abc-user ./file.xml


How to change the owner as well as group of a file?

To change the owner and the group of the file the CHOWN command is 

CHOWN abc-user:xyz-group ./file.xml


How to recursively change owner of all the files and directories inside another directory?

To change the owner recursively use "-r"

CHOWN -r abc-user ./dir/*



If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.



Trending Articles


Incorrect table definition there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

Web Server vs Application Server Difference Explained

Infix To Postfix Conversion Using Stack in Javascript

SQL Overview

Binary Search Tree Insert node Implementation in Javascript

Evaluate Postfix Expressions Using Stack In Javascript

Linked List Size or Count in Javascript

MYSQL Dump - How to take mysql dump or export mysql data