5+ chgrp Command with Examples in Linux

This tutorial explains Linux “chgrp” command, options and its usage with examples.

“chgrp” command is used to change group ownership of a file/directory. This post describes “chgrp” command used in Linux along with usage examples and/or output.

All files in linux belong to an owner, and a group. The owner is set by the “chown” command, and the group by the “chgrp” command.

Usage:
chgrp [OPTION]… GROUP FILE…
chgrp [OPTION]… –reference=RFILE FILE…

Here’s the listing of example usage of command. :

Note:
Before using “chgrp” command, You should be aware of the users existing in your system, otherwise you will get “Invalid user name” Error.
To see the list of the users, You may use(cat /etc/group). But if you still want to add a particular user , then run in the terminal following command and then use “chgrp” command accordingly:

  sudo addgroup user_name_to_add

1. To change the group name of a file(chgrp group_name file_name):

advertisement
advertisement
sanfoundry-> touch 1.txt 
sanfoundry-> ls -l 1.txt 
-rw-rw-r-- 1 himanshu himanshu 0 Jun 12 18:57 1.txt
sanfoundry-> sudo chgrp user 1.txt 
sanfoundry-> ls -l 1.txt 
-rw-rw-r-- 1 himanshu user 0 Jun 12 18:57 1.txt
sanfoundry->

As you can see, Here user for the 1.txt was “himanshu” and after executing the “chgrp” command, user_name has been changed to “user”.

2. To change the group name of a folder(chgrp group_name folder_name):

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
sanfoundry-> mkdir sample_folder
sanfoundry-> ls -l 
drwxrwxr-x 2 himanshu himanshu  4096 Jun 12 19:04 sample_folder
 
sanfoundry-> sudo chgrp user sample_folder/
sanfoundry-> ls -l
drwxrwxr-x 2 himanshu user      4096 Jun 12 19:04 sample_folder

As you can see, Here user for the folder sample_folder was “himanshu” and after executing the “chgrp” command, user_name has been changed to “user”.

3. To change the group name of all contents in a folder(chgrp -R group_name folder_name):
“-R” option is for the recursive changes in the folder.

sanfoundry-> mkdir sample_folder
sanfoundry-> cd sample_folder/
sanfoundry-> touch x y 
sanfoundry-> mkdir folder1
sanfoundry-> cd ..
sanfoundry-> ls -l sample_folder/
total 4
drwxrwxr-x 2 himanshu himanshu 4096 Jun 12 19:09 folder1
-rw-rw-r-- 1 himanshu himanshu    0 Jun 12 19:09 x
-rw-rw-r-- 1 himanshu himanshu    0 Jun 12 19:09 y
sanfoundry-> sudo chgrp -R user sample_folder/
sanfoundry-> ls -l sample_folder/
total 4
drwxrwxr-x 2 himanshu user 4096 Jun 12 19:09 folder1
-rw-rw-r-- 1 himanshu user    0 Jun 12 19:09 x
-rw-rw-r-- 1 himanshu user    0 Jun 12 19:09 y

4. To print the verbose messages when changes made in the ownership(chgrp -c group_name file/folder name):

advertisement
sanfoundry-> touch 1.txt 
sanfoundry-> sudo chgrp -c user 1.txt 
changed group of `1.txt' from himanshu to user

5. To use group name of a file instead of specifying a particular group_name( chgrp -–reference=file1 file2):

sanfoundry-> touch 1.txt 2.txt 
sanfoundry-> sudo chgrp user 1.txt 
sanfoundry-> ls -l 1.txt 2.txt 
-rw-rw-r-- 1 himanshu user     0 Jun 12 19:19 1.txt
-rw-rw-r-- 1 himanshu himanshu 0 Jun 12 19:19 2.txt
sanfoundry-> sudo chgrp --reference=1.txt 2.txt 
sanfoundry-> ls -l 1.txt 2.txt 
-rw-rw-r-- 1 himanshu user 0 Jun 12 19:19 1.txt
-rw-rw-r-- 1 himanshu user 0 Jun 12 19:19 2.txt

As you can see from the above example, 1.txt has group name “user” and with –reference option that has been given to the file “2.txt” and now both shares a common group name,”user”.

6. output a diagnostic for every file processed(chgrp -v group_name file_name):

advertisement
sanfoundry-> touch 1.txt
sanfoundry-> sudo chgrp -v user 1.txt 
changed group of `1.txt' from himanshu to user

7. To change the group name of link files(chgrp –dereference group_name … file_name):
To create a symbolic link for a file, say 1.txt, You have to use ln -s command:

sanfoundry-> touch 1.txt
sanfoundry-> ln -s 1.txt symbolic_link

Here file symbolic_link is the link_name for file 1.txt.
* with “-dereference” option the group name of actual file pointed by symbolic_link gets changed.
* with “-no-dereference” option the group name of symbolic_link gets changed itself.

sanfoundry-> sudo  chgrp --dereference user symbolic_link
sanfoundry-> ls -l symbolic_link 1.txt 
-rw-rw-r-- 1 himanshu user     0 Jun 13 00:43 1.txt
lrwxrwxrwx 1 himanshu himanshu 5 Jun 13 00:43 symbolic_link -> 1.txt
 
sanfoundry-> sudo  chgrp --no-dereference hello symbolic_link
sanfoundry-> ls -l symbolic_link 
lrwxrwxrwx 1 himanshu hello 5 Jun 13 00:43 symbolic_link -> 1.txt

From the above example, You can see that with “–dereference” option the group name of the linked file 1.txt has been changed and group name of symbolic_link remains the same. But with “–no-dereference” option the group name of the symbolic link has been changed itself.

8. To change the group name of symbolic link itself without “–no-dereference” option(chgrp -h group_name link_name):

sanfoundry-> sudo  hello -h user symbolic_link
sanfoundry-> ls -l symbolic_link 
lrwxrwxrwx 1 himanshu hello 5 Jun 13 00:43 symbolic_link -> 1.txt

Some more options for chgrp
* “-f” To suppress most of the error messages
* “-RL” To traverse every symbolic link to a directory encountered

Note:
* As a security measure if you want to give permissions to a command to some group you can use this command.
* “chown” command is used to change ownership as well as group name associated to different one, where as “chgrp” can change only group associated to it.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.