10 chgrp command examples in Linux [Cheat Sheet]

Introduction to chgrp command in Linux

chgrp command is used to change the group ownership of a file or directory in the Linux system. Every file and directory in Linux has an owner and group. chown command can be used to change both owner and group of a file or directory. Every group in the Linux system has a unique name and group ID.

 

chgrp vs chown command comparison

chgrp command changes only group ownership, whereas chown command changes both user and group ownership. chown command can take both user and group as arguments.

Advertisement
$ sudo chown user_name:group _name file_name

Sample Output:

chown command to change owner and group at the same time

For more information on chown command, please refer to our article 15 most used chown command in Linux.

chgrp command takes group name as an argument. The basic syntax of chgrp command would be:

$ sudo chgrp [option] group_name file_name

 

Different examples to use chgrp command

The root permission is required to execute the chgrp command for changing the group ownership of a file or directory. The group name must be valid to use as an argument. You can create a new group by using the groupadd command.

To view the owner and group of a file or directory, you can use ls -l command. Here, test is the file name, deepak is an owner/user and linux is a group.

$ ls -l test
-rw-rw-r-- 1 deepak linux 45 Sep 4 23:07 test

In this article, you will learn to change the group ownership of a file or directory with the help of chgrp command.

Advertisement

 

1. chgrp command to change the group ownership of a file

This is a simple command that changes the group ownership of a file.

$ sudo chgrp group_name file_name

Sample Output:

chgrp command to change the group ownership of file

In the above output, the group owner of the file test is deepak. We changed it to the group linux using chgrp command.

It also takes multiple files as an argument. It will assign the same group to all files.

chgrp command to change ownership of multiple files

 

2. Change the group ownership of the directory using the chgrp command

You can use the chgrp command to change the group ownership of the directory.

$ sudo chgrp group_name dir_name

Sample Output:

chgrp command to change the group ownership of directory

Here, we have changed the group ownership of the directory test_dir from deepak to linux.

You can also use the complete file or directory path instead of the name. Let's change the group ownership of the file hello to deepak using its path.

Advertisement

chgrp command to change group ownership with file path

 

3. chgrp command to display the process information

-v or --verbose option displays the information of every file processed by chgrp command.

$ sudo chgrp -v group_name file_name

OR

$ sudo chgrp --verbose group_name file_name

Sample Output:

chgrp command to display information

It displays the information whether the group ownership is changed or not in a file or directory.

 

4. chgrp command to display the information only when a change is made

It is similar to the previous command. The only difference is that it displays the information only when a change is made in group ownership.

-c or --changes option displays the information if the group ownership is changed in a file or directory.

$ sudo chgrp -c group_name file_name

OR

$ sudo chgrp --changes group_name file_name

Sample Output:

chgrp command to report only when a change is made

 

5. Copy group ownership from one file to another using chgrp command

You can use the group ownership of a file or directory as a reference rather than specifying the group name. --reference=RFILE option allows you to use RFILE's group to change the group ownership of a file or directory.

$ sudo chgrp --reference=RFILE file_name

Sample Output:

chgrp command to change the group ownership from reference file

In the above output, we took the file test as a reference to change the group ownership of a directory test_dir.

 

6. Use chgrp command recursively to operate on files and directories

-R option allows you to change the group ownership of the directory and all of its content recursively. It also includes sub-directories and their files. This command is useful when all the files in the directory need the same group ownership.

$ sudo chgrp -R group_name dir_name

OR

$ sudo chgrp --recursive group_name dir_name

Sample Output:

chgrp command to operate on files and directories recursively

Here, we have changed the group ownership of the directory test_dir and all of its files using -R option.

 

7. Change the group ownership of symbolic link file using chgrp command

A symbolic link file is a type of file that points to another file. To create a symbolic link file, you can use:

$ ln -s source_file symbolic_filename 

Normally, when you change the group ownership of a symbolic link file, the group ownership of the pointed file changes. -h or --no-derefence option allows you to change the group ownership of a symbolic link file.

$ sudo chgrp --no-derefence group_name symbolic_filename

OR

$ sudo chgrp -h group_name symbolic_filename

Sample Output:

chgrp command to change the ownership of symbolic link file

As you can see, the group ownership of a symbolic link file is only changed.

 

8. chgrp command to hide or suppress the error messages

-f, --silent or --quiet option hides the error messages in the output.

$ sudo chgrp -f 

OR

$ sudo chgrp --silent

OR

$ sudo chgrp --quiet

Sample Output:

chgrp command to hide errors in output

But, it does not hide the error messages due to invalid group or syntax.

chgrp command to hide error message

 

9. chgrp command to not operate recursively on the root directory

By default, -R option operates on the root directory(/) recursively and changes the group ownership of all of its content.  --preserve-root option does not allow to operate recursively on the root directory.

$ sudo chgrp --preserve-root 

Sample Output:

chgrp command to not operate recursively on the root directory

 

10. chgrp command to change ownership of source directory linked to symbolic link

By default, when operating recursively on a symbolic link directory, it only changes the group ownership of a symbolic link directory. But with -H option, it changes the group ownership of the source directory and all of its content.

$ sudo chgrp -H -R group_name symbolic_dir

Sample Output:

chgrp command to operate recursively

The group ownership of the symbolic link directory is not changed. The command argument is passed to the source directory. So, the group ownership of the source directory and its files are changed.

 

Conclusion

Now, you can easily change the group ownership of any file or directory in the Linux system. These are the most used chgrp commands in Linux, which have been tested in Ubuntu. You can also use these commands in RHEL, CentOS, Fedora, Debian, and other Linux distributions.

 

What's Next

15 most used usermod command examples in Linux

 

Further Reading

man page for chgrp command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment