5 UNIX / Linux chgrp Command Examples

What is chgrp?
5 chgrp examples
Syntax and Options
Related Commands

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


Related Commands