How to change group ownership in linux

1. Name

chgrp [man page] – change group ownership

2. Synopsis

chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE...

3. Frequently used options in linux

-R, --recursive
       operate on files and directories recursively

4. Examples

Let’s first create same sample directories in /tmp to test chgrp command. The following mkdir command will create two directories: directory1 and directory2 where directory1 is a parent of directory2.

# cd /tmp
 # mkdir -p directory1/directory2

chgrp create directories
Next we can also create a file in /tmp/directory1/directory2

# touch /tmp/directory1/directory2/file
 # ls -ld /tmp/directory1/directory2/
 # ls -l directory1/directory2/

chgrp create file
At this stage as we can see that group ownership is assigned to a root for both directories and file. To change an ownership of directory1 to linuxconfig group we can enter command:

NOTE: the group must exists in order to take ownership of a file or directory

# chgrp linuxconfig /tmp/directory1/
 # ls -ld /tmp/directory1/
 # ls -ld /tmp/directory1/directory2/

change ownership of directory
The group ownership of directory1 has been changed to linuxconfig. To change a group ownership of directory2 and a file within a directory2 we need to use a chgrp’s recursive options -R.

# chgrp -R linuxconfig /tmp/directory1/
 # ls -ld /tmp/directory1/
 # ls -ld /tmp/directory1/directory2/
 # ls -l /tmp/directory1/directory2/

change group ownership of directory and file

Source taken from