5+ Practical “chgrp” Command Usage Examples in Linux

What is chgrp?


5 chgrp Examples

1. To change the group name of a file or directory

You can change the group of a file as shown below,

$ ls -l
-rw-r--r-- 1 john john 210 2011-01-04 16:01 /home/john/sample.txt

$ chgrp user /home/john/sample.txt

$ ls -l
-rw-r--r-- 1 john user 210 2011-01-04 16:01 /home/john/sample.txt

2. To change the group name of link files

Using –derefence option, the group of referent of symbolic link file can be changed and with –no-dereference option, the group of symbolic link file can be changed as shown below.

# the group name of actual file pointed by sym_link gets changed.
$ chgrp --dereference guest sym_link

# the group name of sym_link gets changed.
$ chgrp --no-dereference user sym_link

$ ls -l sym_link richard_readme.txt
lrw-r--r--  1 bala user    4  2011-01-04 15:52 sym_link -> readme.txt
-rw-r--r-- 1 bala guest  20 2011-01-04 15:52 readme.txt

Note : –dereference is the default option.

3. To change the group for list of files

Using -R option, the list of file’s group name can be modified as shown below,

$ chgrp -R guest dir/

$ ls -l dir/
total 8
-rw-r--r-- 1 bala guest 20 2011-01-04 16:50 readme.txt
-rw-r--r-- 1 bala guest 20 2011-01-04 16:50 changes.txt

4. To use the group value of another file instead of specifying explicitly

By using –reference=RFILE option, the group value of RFILE’s gets used instead of specifying the value explicitly.

$ ls -l /home/bala/sample.txt
-rw-r--r-- 1 bala guest 20 2011-01-04 16:50 /home/bala/query.txt

$ ls -l query.txt
-rw-r--r-- 1 steven user 20 2011-01-04 16:50 query.txt

$ chgrp --reference=/home/bala/query.txt query.txt

$ ls -l query.txt
-rw-r--r-- 1 steven guest 20 2011-01-04 16:50 query.txt

5. To print the verbose messages only when changes made

The -c option makes the verbose messages gets printed only when the changed is made in FILE.

$ chgrp -c user sample.txt 
changed group of `sample.txt' to user

Syntax and Options

chgrp [OPTIONS]… GROUP FILE…

Short Option Long Option Option Description
-c –changes to print verbose only when changes made
–dereference to change the group name of referent of symbolic link rather than symbolic link itself
-h –no-dereference to change the group name of symbolic link itself
–no-preserve-root do not treat ‘/’ specially ( default )
–preserve-root fail to operate recursively on ‘/’
-f –silent, –quiet to suppress most of the error messages
–reference=RFLIE to use RFILE’s group name instead of specifying it explicitly
-R –recursive to operate on files and directories recursively
-v –verbose to output a diagnostic for every file processed

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):

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-> 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):

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):

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.

Difference between chown and chgrp

1) 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.

2) Many people say that regular user only able to use chgrp to change the group if the user belongs to them. But it’s not true a user can use chown and chgrpirrespective to change group to one of their group because chown is located in /bin folder so every can use it with some limited access.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s