Linux Commands – chgrp

Linux Commands – chgrp

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.

You can use chgrp -c or –changes options to verbosely describe the action for each FILE whose group actually changes.

You can use chgrp -f or –silent or –quiet options to suppress printing of error messages about files whose group cannot be changed.

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.

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.

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.

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

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.

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.

Hope you have enjoyed this article. In the next blog post, we will discuss ps command in Linux.

 

Leave a Reply

Close Menu