chgrp command in Linux with Examples - GeeksforGeeks

18 Apr, 2019
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
Example:
sudo chgrp -c geeksforgeeks f1
-f : To suppress error messages.Example:sudo chgrp -f geeksforgeeks f2
Example:
sudo chgrp -f geeksforgeeks f2
-v : To describe the action or non-action taken for every File.Example:sudo chgrp -v geeksforgeeks f1
Example:
sudo chgrp -v geeksforgeeks f1
–dereference/ –no-dereference: To change the group name of link files.Example:sudo chgrp --dereference geeksforgeeks symbolic_linkHere 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_linkHere file symbolic_link is the link_file for file f1. With “–no-dereference” option the group name of the symbolic_link itself gets changed.
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.
linux-command
Linux-directory-commands
Linux-file-commands
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
tar command in Linux with examples
Conditional Statements | Shell Script
'crontab' in Linux with Examples
UDP Server-Client implementation in C
Tail command in Linux with examples
Cat command in Linux with examples
touch command in Linux with Examples
echo command in Linux with Examples
Compiling with g++
scp command in Linux with Examples