chown command – change the owner of a file
You must be either the root user or the owner of the file in order to change the group ownership.
To change the group and owners of files and folders on the linux server use the below command
Set the file’s owner:
$ chown username filename
where username is the new owner of the file and filename is the file for which ownership need to be changed.
You can also set the file’s group at the same time. If the user name is followed by a colon and a group name, the file’s group will be changed as well.
$ chown username:usergroup filename
You can set the owner of a directory exactly the same way you set the owner of a file:
$ chown username dirname
In order to set the ownership of a directory and all the files in that directory (recursive), you’ll need the -R
option:
$ chown -R username dirname
R stands for recursive because this command will recursively change the ownership of directories and their contents
Verbose mode
$ chown -v username somefile
changed ownership of 'somefile' to username
Here, v stands for verbose. If you use the -v
option, chown
will list what it did (or didn’t do) to the file.
The verbose mode is especially useful if you change the ownership of several files at once. For example, this could happen when you do it recursively:
$ chown -Rv username somedir
changed ownership of 'dirname/' to username
changed ownership of 'dirname/file1' to username
changed ownership of 'dirname/file2' to username
chgrp – change the group ownership of a file
You must be either the root user or the owner of the file in order to change the group ownership.
chgrp
works the same way as chown
does, except it changes the file’s user group instead of the owner
$ chgrp usergroup filename
where usergroup is the newgroup to which the filename need to be assigned
after execution of the command, the file’s group has changed to usergroup
, the file’s owner will still be the same.
The options of using chgrp
are the same as using chown
. So, for example, the -R
and -v
options will work with it just like they worked with chown
:
$ chgrp -Rv usergroup dirname
changed group of 'dirname/' to usergroup
changed group of 'dirname/file1' to usergroup
changed group of 'dirname/file2' to usergroup