Linux Chgrp Command with Examples

change group in linux

In Linux permissions, all files including directories are associated with the user and group ownership. Additionally, the file and directories and usually assigned permissions that determine access rights such as read, write and execute that ultimately determine who has what rights to access them.

In this tutorial, I will show you how to change the group ownership of a file or directory using the chgrp command.  The Linux change group command, which is known as chgrp is used to alter the group name that a file or directory belongs to. Unlike the chown command that requires you to specify both the user and the group name, chgrp requires just the group.

Syntax for chgrp command

The syntax for changing the group that a file belongs to is quite easy. Simply invoke the chgrp command and thereafter define the group name followed the objects name.

$  chgrp OPTION group_name  /path/to/file or directory

The group_name attribute represents the new group that the file or directory will acquire. Additionally, you can specify the GID (group ID) instead of the group name.  In that case, you need to append the GID with a plus sign (+).

Change group of a file using chgrp command

In this section, we will look at how to change group ownership of a file. Before changing the group of a file using the chgrp command, it's recommended to first verify current group that the file belongs to. This can easily be accomplished using the ls command to print the file permissions of the file as shown:

$ ls -l

The example below prints out the file permissions of a file called settings.py inside the docs directory.

chgrp command change group linux

From the output above, the file belongs to the user 'Winnie' and group 'Winnie'. To change the group of the file from 'Winnie' to 'linoxide' group for example, run the chgrp command as shown:

$ chgrp linoxide settings.py

However, in most cases, when changing the group of a file or directory as a regular user, you will encounter a chgrp operation not permitted error as displayed in the image below. The error indicates that the user does not have sufficient permissions to change the group ownership of the file.

chgrp Permissions error

A workaround to this challenge is to add the regular user to the sudoers group and invoke the sudo command as shown below:

$ sudo chgrp linoxide settings.py

linux chgrp command

From the output above, we can see that we have been successful in changing the group to 'linoxide' after invoking the sudo command and submitting the password.

Furthermore, you can specify multiple files as arguments when using the chgrp command as follows:

$ sudo chgrp group_name file_1 file_2 file_3

In the example below, we have 3 files: settings.py, file1.txt and file2.txt all belonging to the group 'Winnie'. Let's now change the group ownership to 'linoxide'.  The syntax will be:

$ sudo chgrp linoxide settings.py file1.txt file2.txt

chgrp Linux command

Change group ownership recursively

Sometimes, you may need to recursively change the group of all the files and subdirectories of a directory. To recursively change the group name, use the -R flag as shown in the syntax below.

$ sudo chgrp group-name directory

For example, to change the ownership of /var/www/html/nextcloud  directory and its contents  to www-data group, run the command:

$ sudo chgrp -R www-data /var/www/html/nextcloud

Change group ownership of symbolic links

Using the recursive option does not traverse the symbolic links and therefore,  the symbolic links will retain their group ownership. To apply the group ownership to the symbolic links, pass the -h parameter as shown:

$ sudo chgrp -Rh www-data /var/www/html/nextcloud

Conclusion

In this tutorial, we walked you through the chgrp command in Linux. While you can achieve the same with the chown command, the chgrp command offers a much simpler way of doing so.

Some might ask why we have chgrp when chown command exists. Chgrp code was built much older, chown initially couldn't setup group.

Note: Chown to only change group chown :group is still not portable or standard on many platforms.

Thanks for reading and please provide your feedback on the below comment section.

Winnie Ondara 3:15 am

Comments

Your email address will not be published. Required fields are marked *