Open In App

chgrp command in Linux with Examples

Last Updated : 15 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The `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 of `chgrp` command in Linux

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 use sudo. In order to add a new group, we can use:

sudo addgroup geeksforgeeks

Examples of `chgrp` command in Linux

Changing Group Ownership of a Single File

To change the group ownership of a file.

sudo chgrp geeksforgeeks abc.txt
For a single File

For a single 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 the same as the owner under which the file was created.

Changing Group Ownership of Multiple Files

The ‘chgrp’ command can also handle multiple files at once. For instance:

chgrp developers file1.txt file2.txt file3.txt

Here, ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’ will all be assigned to the ‘developers’ group.

Changing Group Ownership of a Directory or Folder

To change the group ownership of a folder.

sudo chgrp geeksforgeeks GFG
For directory or folder

For directory or folder

Recursively change the group ownership of a folder

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

sudo chgrp -R geeksforgeeks GFG
Recursively

Recursively

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.

Using the groupname of a reference file

Using the groupname of a reference file to change the group of another file or folder.

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

reference file

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 Available in `chgrp` command in Linux

`-c` or `–changes` Option

To describe the action for each File whose group actually changes.

Example:

sudo chgrp -c geeksforgeeks f1
-c

-c

`-f` Option

To suppress error messages.

Example:

sudo chgrp -f geeksforgeeks f2
-f

-f

`-v` Option

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

Example:

sudo chgrp -v geeksforgeeks f1
-v

-v

`–dereference` or `–no-dereference` Option

To change the group name of link files.

Example:

sudo chgrp --dereference geeksforgeeks symbolic_link

--dereference

–dereference

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.

Conclusion

In this article we discussed `chgrp` command in Linux which is a powerful tool for changing the group ownership of files and directories. It allows users to modify the group ownership for single or multiple files, as well as entire directories recursively. By using various options, users can efficiently manage access permissions and enhance the security of their file systems. The chgrp command proves to be an essential utility for system administrators and users, providing seamless control over group ownership and access control within the Linux environment.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads