Linux Commands – chgrp
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed chown command in Linux which is used to change a file or directory owner or group or both.
https://cloudaffaire.com/linux-commands-chown/
In this blog post, we will discuss chgrp command in Linux. chgrp command is used to change the group ownership of a file or directory. chgrp command accepts both a group name or group id as an argument. You can change the group ownership of a file or directory provided you are the member of the target group or have root/sudo privileges.
Linux Commands – chgrp:
You can use chgrp command to change the group ownership of a file or directory in Linux.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
############################ ## Linux Commands | chgrp ## ############################ ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------ ## chgrp ##------ ## chgrp [option]... GROUP FILE... ## chgrp [option]... --reference=RFILE FILE... ## chgrp command sudo groupadd -g 1111 mygroup1 ## create some groups sudo groupadd -g 2222 mygroup2 mkdir mydir ## create some file and dir touch myfile ls -l ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 2 02:05 mydir ## -rw-rw-r-- 1 debjeet cloudaffaire 0 Jun 2 02:05 myfile sudo chgrp mygroup1 myfile ## change myfile group ownership to mygroup1 sudo chgrp 2222 mydir ## chgrp command also accepts group-id ls -l ## drwxrwxr-x 2 debjeet mygroup1 6 Jun 2 02:05 mydir ## -rw-rw-r-- 1 debjeet mygroup2 0 Jun 2 02:05 myfile |
You can use chgrp -c or –changes options to verbosely describe the action for each FILE whose group actually changes.
1 2 3 4 |
## chgrp -c or --changes options sudo chgrp -c mygroup2 myfile ## changed group of ‘myfile’ from mygroup1 to mygroup2 |
You can use chgrp -f or –silent or –quiet options to suppress printing of error messages about files whose group cannot be changed.
1 2 3 4 5 6 7 8 9 10 11 |
## chgrp -f or --silent or --quiet options chgrp mygroup1 myfile ## will give error without sudo ## chgrp: changing group of ‘myfile’: Operation not permitted chgrp -f mygroup1 myfile ## with -f option, supress the error message ls -l myfile ## group does not get changed ## -rw-rw-r-- 1 debjeet mygroup2 0 Jun 2 02:11 myfile rm -rf my* |
You can use chgrp –dereference option to not change the ownership of a symbolic link, instead change the ownership of the file or directory that the symbolic link refers to. This is the default behavior of chgrp command and works the same without this option.
You can use chgrp -h or –no-dereference options to change the ownership of symbolic links themself instead of what the symbolic link refers to. This option relies on the ‘lchown’ system call. On systems that do not provide the ‘lchown’ system call, ‘chgrp’ fails when a file specified on the command line is a symbolic link.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
## chgrp --dereference option ## chgrp -h or --no-dereference options echo "hello" > myfile ln -s myfile mylink ## create a symbolic link ls -l ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:23 myfile ## lrwxrwxrwx 1 debjeet cloudaffaire 6 Jun 2 02:23 mylink -> myfile sudo chgrp mygroup1 mylink ## follows symbolic link ls -l ## -rw-rw-r-- 1 debjeet mygroup1 6 Jun 2 02:23 myfile ## lrwxrwxrwx 1 debjeet cloudaffaire 6 Jun 2 02:23 mylink -> myfile sudo chgrp --dereference mygroup2 mylink ## follows symbolic link ls -l ## -rw-rw-r-- 1 debjeet mygroup2 6 Jun 2 02:23 myfile ## lrwxrwxrwx 1 debjeet cloudaffaire 6 Jun 2 02:23 mylink -> myfile sudo chgrp -h mygroup1 mylink ## does not follows symbolic link ls -l ## -rw-rw-r-- 1 debjeet mygroup2 6 Jun 2 02:23 myfile ## lrwxrwxrwx 1 debjeet mygroup1 6 Jun 2 02:23 mylink -> myfile rm -rf my* |
You can use chgrp –reference=REF_FILE option to change the group of each FILE to be the same as that of REF_FILE. If REF_FILE is a symbolic link, do not use the group of the symbolic link, but rather that of the file it refers to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## chgrp --reference=REF_FILE option touch myfile{1,2} ## create some files ls -l ## -rw-rw-r-- 1 debjeet cloudaffaire 0 Jun 2 02:31 myfile1 ## -rw-rw-r-- 1 debjeet cloudaffaire 0 Jun 2 02:31 myfile2 sudo chgrp mygroup1 myfile1 ## change myfile1 group ownership sudo chgrp --reference=myfile1 myfile2 ## set myfile2 group owner same as myfile1 ls -l ## -rw-rw-r-- 1 debjeet mygroup1 0 Jun 2 02:31 myfile1 ## -rw-rw-r-- 1 debjeet mygroup1 0 Jun 2 02:31 myfile2 rm -rf my* |
You can use chgrp -R or –recursive options to recursively change the group ownership of directories and their contents. By default, chgrp does not change the ownership of directory contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## chgrp -R or --recursive options mkdir -p mydir1/mydir2/mydir3 ## create some directories sudo chgrp mygroup1 mydir1 ## only changes mydir1 ownership ls -Rl ## drwxrwxr-x 3 debjeet mygroup1 20 Jun 2 02:42 mydir1 ## drwxrwxr-x 3 debjeet cloudaffaire 20 Jun 2 02:42 mydir2 ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 2 02:42 mydir3 sudo chgrp -R mygroup2 mydir1 ## change group ownership recursively ls -Rl ## drwxrwxr-x 3 debjeet mygroup2 20 Jun 2 02:42 mydir1 ## drwxrwxr-x 3 debjeet mygroup2 20 Jun 2 02:42 mydir2 ## drwxrwxr-x 2 debjeet mygroup2 6 Jun 2 02:42 mydir3 rm -rf my* |
You can use chgrp -H or -L or -P options with -R option to control the behavior of symbolic link in recursive mode if they refer to a directory.
- -H : follow the symbolic link provided as command line argument, but do not follow any other symbolic link during traversing
- -L : follow all symbolic links
- -P : Do not follow any symbolic link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
## chgrp -H or -L or -P options with -R option mkdir -p ~/mydir1/mydir11 ~/mydir2 ## create some files,directories and symbolic links ln -s ~/mydir1 mylink1 ln -s ~/mydir2 ~/mydir1/mylink2 echo "hello" > ~/mydir1/myfile1 echo "world" > ~/mydir2/myfile2 tree ## current directory structure ## . ## ├── mydir1 ## │ ├── mydir11 ## │ ├── myfile1 ## │ └── mylink2 -> /home/debjeet/mydir2 ## ├── mydir2 ## │ └── myfile2 ## └── mylink1 -> /home/debjeet/mydir1 ls -Rl ## current permissions ## drwxrwxr-x 3 debjeet cloudaffaire 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 debjeet cloudaffaire 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 debjeet cloudaffaire 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:57 myfile2 sudo chgrp -RH mygroup1 mylink1 ## follow the symbolic link provided in the argument ## but do not follow any other symbolic link there after ls -Rl ## drwxrwxr-x 3 debjeet mygroup1 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 debjeet mygroup1 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 debjeet mygroup1 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 debjeet mygroup1 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 debjeet cloudaffaire 6 Jun 2 02:57 myfile2 sudo chgrp -RL mygroup2 mylink1 ## follow all symbolic links ls -Rl ## drwxrwxr-x 3 debjeet mygroup2 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 debjeet mygroup2 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 debjeet mygroup2 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 debjeet mygroup2 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 debjeet mygroup2 6 Jun 2 02:57 myfile2 sudo chgrp -RP mygroup1 mylink1 ## do not follow any symbolic link ls -Rl ## drwxrwxr-x 3 debjeet mygroup2 51 Jun 2 02:56 mydir1 ## drwxrwxr-x 2 debjeet mygroup2 21 Jun 2 02:57 mydir2 ## lrwxrwxrwx 1 debjeet mygroup1 21 Jun 2 02:55 mylink1 -> /home/debjeet/mydir1 ## drwxrwxr-x 2 debjeet mygroup2 6 Jun 2 02:55 mydir11 ## -rw-rw-r-- 1 debjeet mygroup2 6 Jun 2 02:56 myfile1 ## lrwxrwxrwx 1 debjeet cloudaffaire 21 Jun 2 02:55 mylink2 -> /home/debjeet/mydir2 ## -rw-rw-r-- 1 debjeet mygroup2 6 Jun 2 02:57 myfile2 rm -rf my* |
You can use chgrp –preserve-root option with -R option to prevent any attempt to recursively change the root directory (/). It’s a failsafe mechanism of OS to prevent recursively changing ownership in the root directory.
1 2 3 4 5 |
## chgrp --preserve-root option chgrp -cfR --preserve-root mygroup1 / ## no changes, just for fun ## chgrp: it is dangerous to operate recursively on ‘/’ ## chgrp: use --no-preserve-root to override this failsafe |
You can use chgrp –no-preserve-root option with -R option to cancel the effect of any preceding ‘–preserve-root’ option. This is a dangerous command and should not be executed. The below example is given just for the demo, do not execute.
1 2 3 4 5 6 7 |
## chgrp --no-preserve-root option chgrp -R --no-preserve-root mygroup1 / ## do not execute ## given just as an example sudo groupdel mygroup1 ## delete groups sudo groupdel mygroup2 |
Hope you have enjoyed this article. In the next blog post, we will discuss ps command in Linux.