Signup/Sign In

How to Change Group Ownership (chgrp command)

In Linux, if we want to change the group ownership of a file or a directory, we use the chgrp command.

A file system object has three sets of access permissions, one set for the owner, one set for the group, and one set for others. We know that using chown we can change the ownership but if the requirement is to only change the group of a file or directory, then we can use chgrp to change the group instead of changing ownership.

To change the group identifier, you must have one of the following authorities:

  • The current user has *ALLOBJ special authority.
  • The current user is the owner of the file and either one of the following:
    - The primary group of the job is the group.
    - One of the supplemental groups of the job is a group.
  • In addition, the current user must have *USE authority profile specified by the group.

You can only change a group to a group you are a member of.

General syntax of the chgrp command -

chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...

Brief description of options available with the chgrp command.

Option Description
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is the default ), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's group rather than specifying a GROUP value
-R, --recursive operate on files and directories recursively
-H if a command-line argument is a symbolic link to a directory, traverse it
-L traverse every symbolic link to a directory encountered
-P do not traverse any symbolic links (default)
--help display this help and exit
--version output version information and exit

chgrp command usage and cheatsheet

1. -f (quiet )- change the group of files and hide error messages

For changing groups, first, we need to create a group "hope" then change it.

Note: It's not showing any Operation not permitted error.

create a group in Linux

Change the group "hope" for the directory "studytonight" in Linux

change the group in Linux

Recursively change the group ownership for snow.txt file studytonight to hope group in Linux

Recursively change the group ownership in Linux

# Change the owner group of a symbolic link:
chgrp -h group path/to/symlink

# Change the owner group of a file/directory to match a reference file:
chgrp --reference=path/to/reference_file path/to/file_or_directory

Conclusion

In this tutorial, we covered how to change the group ownership of a file or directory using the chgrp (change group) command in the Linux operating system with available options and suitable examples.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.