chgrp command in Linux with Examples

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.

Syntax:

chgrp [OPTION]… GROUP FILE…
chgrp [OPTION]… –reference=RFILE FILE…

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:

sudo addgroup geeksforgeeks

Example 1: To change the group ownership of a file.

sudo chgrp geeksforgeeks abc.txt

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.

sudo chgrp geeksforgeeks GFG

Example 3: To recursively change the group ownership of a folder and all of its contents.

sudo chgrp -R geeksforgeeks GFG

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.

sudo chgrp -R --reference=abc.txt GFG

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.

Options:

  • -c or –changes : To describe the action for each File whose group actually changes.

    Example:

    sudo chgrp -c geeksforgeeks f1

  • -f : To suppress error messages.

    Example:

    sudo chgrp -f geeksforgeeks f2

  • -v : To describe the action or non-action taken for every File.

    Example:

    sudo chgrp -v geeksforgeeks f1

  • –dereference/ –no-dereference: To change the group name of link files.

    Example:

    sudo chgrp --dereference geeksforgeeks symbolic_link

    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.

    Example:

    sudo chgrp --dereference geeksforgeeks symbolic_link

    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.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.