Linux chgrp Command | Linux Change Group

Linux chgrp command is used to change the group ownership of a file or directory. In the Linux file system, each file is related to corresponding owner and group, and has read, write, and execute permission.

Syntax:

Options:

The chgrp command supports the following command-line options:

-c, --changes: It is like 'verbose' option but, it is reported only when a change is made.

-f, --silent, --quiet: It is used to suppress most error messages.

-v, --verbose: It is used to display a diagnostic for every file processed.

--dereference: It is used to affect each symbolic link's significance, rather than the symbolic link itself.

-h, --no-dereference: It is used to alter the symbolic links instead of any referenced file

--no-preserve-root: It is used for not treating the '/' especially.

--preserve-root: It is used when the default command is failed to operate recursively on '/.'

--reference=RFILE: It is used to use RFILE's group rather than a specific group.

-R, --recursive: It is used to operate on files and directories recursively. The option specifies the traversing of the '-R' option.

-H: It is used to traverse a symbolic link to a directory.

-L: It is used to traverse every symbolic link to a directory encountered.

-P: If this option is specified, it will not traverse any symbolic links (default)

--help: It is used to display the help manual having the brief information of supported command-line options.

--version: It is used to display version information.

Examples of the chgrp Command

Let's see the following examples of the chgrp command:

  • Change the group ownership of a file
  • Change the group ownership of a folder
  • Recursively change the group ownership of a folder and its contents
  • Change group by using the group name of a reference file

Change the Group Ownership of a File

We can change the group of any specific file. To change the group ownership of a file, execute the command as follows:

The above command will change the group of file 'Demo1.txt' to Javatpoint. Consider the below output:

Linux chgrp Command

As we can see from the above output, the 'groups' command is used to list the available groups in the system. And, the 'chgrp' command executed with the sudo because it needs sudo privilege.

Change the group ownership of a folder

To change the group ownership of a folder, execute the 'chgrp' command with the folder name as follows:

The above command will change the group ownership of the 'Newdirectory' folder. Consider the below output:

Linux chgrp Command

If the chgrp command is successfully executed, it will not produce any output. To confirm the changes, execute the "ls -l" command.

Recursively change the group ownership of a folder and its content

We can change the group ownership of a file or folder recursively by applying some set of rules. To recursively change the group ownership of a folder and its content, execute the command as follows:

Consider the below output:

Linux chgrp Command

Change group by using the group name of a reference file

We can change the group of a file by using the group name of a reference file. To change the group of another file using the reference of any other file or directory, execute the command as follows:

The above command will change the group of 'ref.txt' by using the reference of 'Nwdirectory.' Consider the below output:

Linux chgrp Command