A newly created file is owned by the user who creates the file. By default, new file has a group ownership which is the primary group of the user creating the file.
How to change ownership of file
File ownership can be changed with the chown command.
Ex. Change ownership of file file1 to ownername, the following command could be used.
chown ownername file1
How to change ownership of file Recursively
To change the ownership of an entire directory, chown can be used with –Roption recursively change.
Ex. Following command would grant ownership of file1 and all files and sub-directories within it to ownername.
chown –R ownername file1
How to change group ownership using chown command
The chown command can be used to change group ownership of a file by preceding the group name with a colon (:).
Ex. The following command will change the group ownership of file file1 to group groupname.
chown :groupname file1
How to change owner and group owner at same time
The chown command can be used to change both owner and group at the same time by using the syntax owner:group.
Ex. To change the ownership of file1 to ownername and group togroupname.
chown ownername:groupname file1
Note: Only root can change the ownership of a file. Group ownership, can be set by root or the file’s owner. root can grant ownership to an group, while non-root users can grant ownership only to group they belongs to.
How to change group ownership of file using chgrp command
The chgrp command can be used to change group ownership of file file1.
Ex. To change the group ownership of file1 to groupname.
chgrp groupname file1