Chgrp Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

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.

The chgrp command was originally developed as part of the Unix operating system by AT&T Bell Laboratories.

~/codeFactory$ chgrp --help 
Usage: chgrp [OPTION]... GROUP FILE...
  or:  chgrp [OPTION]... --reference=RFILE FILE...
Change the group of each FILE to GROUP.
With --reference, change the group of each FILE to that of RFILE.

  -c, --changes          like verbose but report only when a change is made
  -f, --silent, --quiet  suppress most error messages
  -v, --verbose          output a diagnostic for every file processed
      --dereference      affect the referent of each symbolic link (this is
                         the default), rather than the symbolic link itself
  -h, --no-dereference   affect symbolic links instead of any referenced file
                         (useful only on systems that can change the
                         ownership of a symlink)
      --no-preserve-root  do not treat '/' specially (the default)
      --preserve-root    fail to operate recursively on '/'
      --reference=RFILE  use RFILE's group rather than specifying a
                         GROUP value
  -R, --recursive        operate on files and directories recursively

The following options modify how a hierarchy is traversed when the -R
option is also specified.  If more than one is specified, only the final
one takes effect.

  -H                     if a command line argument is a symbolic link
                         to a directory, traverse it
  -L                     traverse every symbolic link to a directory
                         encountered
  -P                     do not traverse any symbolic links (default)

      --help     display this help and exit
      --version  output version information and exit

Examples:
  chgrp staff /u      Change the group of /u to "staff".
  chgrp -hR staff /u  Change the group of /u and subfiles to "staff".

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report chgrp translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/chgrp>
or available locally via: info '(coreutils) chgrp invocation'

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 cf

1. To change the group ownership of a file.

~# ls -l                                                                                                                           
total 8                                                                                                                                                
drwxr-xr-x 1 root root 0 Aug 19 16:14 dir1                                                                                                             
drwxr-xr-x 1 root root 0 Aug 19 16:14 dir2                                                                                                             
-rw-r--r-- 1 root root 8 Aug 19 16:15 file1                                                                                                            
-rw-r--r-- 1 root root 8 Aug 19 16:16 file2 

~# chgrp cf file1

~# ls -l                                                                                                                           
total 8                                                                                                                                                
drwxr-xr-x 1 root root 0 Aug 19 16:14 dir1                                                                                                             
drwxr-xr-x 1 root root 0 Aug 19 16:14 dir2                                                                                                             
-rw-r--r-- 1 root cf   8 Aug 19 16:15 file1                                                                                                            
-rw-r--r-- 1 root root 8 Aug 19 16:16 file2

2. To change the group ownership of a folder.

~# chgrp cf dir1

~# ls -l                                                                                                                           
total 8                                                                                                                                                
drwxr-xr-x 1 root cf   0 Aug 19 16:14 dir1                                                                                                             
drwxr-xr-x 1 root root 0 Aug 19 16:14 dir2                                                                                                             
-rw-r--r-- 1 root cf   8 Aug 19 16:15 file1                                                                                                            
-rw-r--r-- 1 root root 8 Aug 19 16:16 file2  

3. To recursively change the group ownership of a folder and all of its contents.

~# ls -l                                                                                                                           
total 8                                                                                                                                                
drwxr-xr-x 1 root cf   24 Aug 19 16:23 dir1                                                                                                            
drwxr-xr-x 1 root root  0 Aug 19 16:14 dir2                                                                                                            
-rw-r--r-- 1 root cf    8 Aug 19 16:15 file1                                                                                                           
-rw-r--r-- 1 root root  8 Aug 19 16:16 file2     

~# ls -l dir1/                                                                                                                     
total 8                                                                                                                                                
-rw-r--r-- 1 root root 8 Aug 19 16:23 f1.txt                                                                                                           
-rw-r--r-- 1 root root 8 Aug 19 16:23 f2.txt  

~# chgrp -R cf dir1/

~# ls -l dir1/                                                                                                                     
total 8                                                                                                                                                
-rw-r--r-- 1 root cf 8 Aug 19 16:23 f1.txt                                                                                                             
-rw-r--r-- 1 root cf 8 Aug 19 16:23 f2.txt

4. Using the groupname of a reference file to change the group of another file or folder.

~# ls -l                                                                                                                           
total 8                                                                                                                                                
drwxr-xr-x 1 root cf   24 Aug 19 16:23 dir1                                                                                                            
drwxr-xr-x 1 root root  0 Aug 19 16:14 dir2                                                                                                            
-rw-r--r-- 1 root cf    8 Aug 19 16:15 file1                                                                                                           
-rw-r--r-- 1 root root  8 Aug 19 16:16 file2

~# ls -l dir1                                                                                                                      
total 8                                                                                                                                                
-rw-r--r-- 1 root cf 8 Aug 19 16:23 f1.txt                                                                                                             
-rw-r--r-- 1 root cf 8 Aug 19 16:23 f2.txt

~# chgrp -R --reference=file2 dir1

~# ls -l                                                                                                                           
total 8                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:23 dir1                                                                                                            
drwxr-xr-x 1 root root  0 Aug 19 16:14 dir2                                                                                                            
-rw-r--r-- 1 root cf    8 Aug 19 16:15 file1                                                                                                           
-rw-r--r-- 1 root root  8 Aug 19 16:16 file2 

~# ls -l dir1                                                                                                                      
total 8                                                                                                                                                
-rw-r--r-- 1 root root 8 Aug 19 16:23 f1.txt                                                                                                           
-rw-r--r-- 1 root root 8 Aug 19 16:23 f2.txt

Options:

  • -c or –changes : To describe the action for each File whose group actually changes.
~# ls -l                                                                                                                             
total 4                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root root  7 Aug 19 16:40 file1

~# chgrp -c cf file1                                                                                                                 
changed group of 'file1' from root to cf

~# ls -l                                                                                                                             
total 4                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root cf    7 Aug 19 16:40 file1 
  • -f : To suppress error messages.
~# chgrp cf file2                                                                                                                    
chgrp: cannot access 'file2': No such file or directory

~# chgrp -f cf file2

~# ls -l                                                                                                                             
total 4                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root cf    7 Aug 19 16:40 file1
  • -v : To describe the action or non-action taken for every File.
~# ls -l                                                                                                                             
total 4                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root root  7 Aug 19 16:40 file1  

~# chgrp -v cf file1                                                                                                                 
changed group of 'file1' from root to cf

~# ls -l                                                                                                                             
total 4                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root cf    7 Aug 19 16:40 file1
  • –dereference/ –no-dereference: To change the group name of link files.
~# ls -l                                                                                                                             
total 4                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root root  7 Aug 19 16:40 file1

~# ln -s file1 symbolic_link

~# ls -l                                                                                                                             
total 8                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root root  7 Aug 19 16:40 file1                                                                                                           
lrwxrwxrwx 1 root root  5 Aug 19 16:51 symbolic_link -> file1

~# chgrp --dereference cf symbolic_link

~# ls -l                                                                                                                             
total 8                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root cf    7 Aug 19 16:40 file1                                                                                                           
lrwxrwxrwx 1 root root  5 Aug 19 16:51 symbolic_link -> file1

~# ls -l                                                                                                                             
total 8                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root cf    7 Aug 19 16:40 file1                                                                                                           
lrwxrwxrwx 1 root root  5 Aug 19 16:51 symbolic_link -> file1

~# chgrp --no-dereference cf symbolic_link

~# ls -l                                                                                                                             
total 8                                                                                                                                                
drwxr-xr-x 1 root root 24 Aug 19 16:41 dir1                                                                                                            
-rw-r--r-- 1 root cf    7 Aug 19 16:40 file1                                                                                                           
lrwxrwxrwx 1 root cf    5 Aug 19 16:51 symbolic_link -> file1

Leave a comment