chgrp command in Linux is used to change the group ownership of a file or directory. All files in Linux belong to an owner and a group. You can set the owner by using “chown” command, and the group by the “chgrp” command.
Note: First we need to have administrator permission to add or delete groups. We can Login as root for this purpose or using sudo. In order to add a new group we can use:
Example 1: To change the group ownership of a file.
Here the group name of the file abc.txt was changed from kcVirtual to geeksforgeeks. Note that when files are created the groupname of the file is same as the owner under which the file was created.
Example 2: To change the group ownership of a folder.
Example 3: To recursively change the group ownership of a folder and all of its contents.
As we can see the group of the folder GFG and its contents F1, F2 was all kcvirtual initially and they were changed to geeksforgeeks with the single command.
Example 4: Using the groupname of a reference file to change the group of another file or folder.
The groupname of the reference file abc.txt was used to recursively change the group of the folder GFG and all its contents using the –reference option.
-c or –changes : To describe the action for each File whose group actually changes.
-f : To suppress error messages.
-v : To describe the action or non-action taken for every File.
–dereference/ –no-dereference: To change the group name of link files.
Here file symbolic_link is the link_file for file f1. With “–dereference” option the group name of actual file pointed by symbolic_link gets changed.
Here file symbolic_link is the link_file for file f1. With “–no-dereference” option the group name of the symbolic_link itself gets changed.