Linux chgrp Command
The Linux chgrp (short for "change group") command is used to change the group ownership of files or directories.
Unlike the chown command, chgrp allows regular users to change the group of a file as long as they are a member of that group.
In the UNIX system family, the control of file or directory permissions is managed by the owner and the group. You can use the chgrp command to change the group ownership of files and directories, and you can use either the group name or the group ID.
Syntax
chgrp [-cfhRv][--help][--version][group][file or directory...]
or
chgrp [-cfhRv][--help][--reference=<reference file or directory>][--version][file or directory...]
Parameter Description
-c or --changes: Similar to the "-v" parameter, but only reports changes.
-f or --quiet or --silent: Does not display error messages.
-h or --no-dereference: Modifies only symbolic links and does not affect any associated files.
-R or --recursive: Recursively processes all files and subdirectories under the specified directory.
-v or --verbose: Displays the execution process of the command.
--help: Online help.
--reference=<reference file or directory>: Sets the group ownership of the specified file or directory to be the same as the reference file or directory.
--version: Displays version information.
Examples
Example 1: Change the group ownership of a file:
chgrp -v bin log2012.log
Output:
[root@localhost test]# ll
---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log
[root@localhost test]# chgrp -v bin log2012.log
The group of "log2012.log" has been changed to bin.
[root@localhost test]# ll
---xrw-r-- 1 root bin 302108 11-13 06:03 log2012.log
Explanation: The log2012.log file has been changed from the root group to the bin group.
Example 2: Change the group ownership of a file based on a specified file:
chgrp --reference=log2012.log log2013.log
Output:
[root@localhost test]# ll
---xrw-r-- 1 root bin 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
[root@localhost test]# chgrp --reference=log2012.log log2013.log
[root@localhost test]# ll
---xrw-r-- 1 root bin 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
Explanation: Changes the group ownership of the log2013.log file to match the group ownership of the reference file log2012.log.