Categories
Life hack

How to use the chgrp command on linux

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:

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.

How to use the chgrp command on linux

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.

How to use the chgrp command on linux

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

How to use the chgrp command on linux

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.

How to use the chgrp command on linux

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:

-f : To suppress error messages.

Example:

How to use the chgrp command on linux
-v : To describe the action or non-action taken for every File.

Example:

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

Example:

How to use the chgrp command on linux

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:

How to use the chgrp command on linux

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.