In Linux permissions, all files (including directories) are associated with user and group ownership. In addition, files and directories and the commonly assigned permissions determine access rights, such as read, write, and execute, and ultimately determine who has access to them.
In this tutorial, I will show you how to use the chgrp command to change the group ownership of a file or directory. The Linux change group command, called
chgrp Used to change the name of the group to which the file or directory belongs. Unlike the chown command that requires you to specify the user name and group name at the same time,
chgrp Only the group is needed.
Syntax of chgrp command
Changing the syntax of the group to which the file belongs is very simple. Just call
chgrp Command, and then define the group name after the object name.
$ chgrp OPTION group_name /path/to/file or directory
The group_name attribute indicates the new group that the file or directory will get. In addition, you can specify the GID (Group ID) instead of the group name. In this case, you need to append a plus sign (+) after the GID.
Use the chgrp command to change the file group
In this section, we will study how to change the group ownership of files. Before using the chgrp command to change the file group, it is recommended to verify the current group to which the file belongs. The file permission for printing files using the ls command is easy to implement, as follows:
$ ls -l
The following example prints out the file permissions of the file named
settings.py In the docs directory.
From the above output, the file belongs to user ‘Winnie’ and group ‘Winnie’. For example, to change the file group from the “Winnie” group to the “linoxide” group, run the chgrp command as follows:
$ chgrp linoxide settings.py
However, in most cases, when you change the file or directory group as a normal user, you will encounter
chgrp operation not permitted Error, as shown in the figure below. This error indicates that the user does not have sufficient permissions to change the group ownership of the file.
The solution to this problem is to add ordinary users to the sudoers group and call the sudo command as follows:
$ sudo chgrp linoxide settings.py
From the above output, we can see that after calling the sudo command and submitting the password, the group has been successfully changed to “linoxide”.
In addition, when using the chgrp command, you can specify multiple files as parameters, as follows:
$ sudo chgrp group_name file_1 file_2 file_3
In the example below, we have 3 files:
file2.txt All belong to the “Winnie” group. Now let’s change the group ownership to “linoxide”. The syntax is:
$ sudo chgrp linoxide settings.py file1.txt file2.txt
Change group ownership recursively
Sometimes, you may need to recursively change all files and subgroups of a directory. To change the group name recursively, use
-R Mark, as shown in the following syntax.
$ sudo chgrp group-name directory
For example, change ownership
/var/www/html/nextcloud Directory and its contents
www-data group, Run the command:
$ sudo chgrp -R www-data /var/www/html/nextcloud
Change symbolic link group ownership
Using the recursive option will not traverse symbolic links, therefore, symbolic links will retain their group ownership. To apply group ownership to symbolic links, pass
-h The parameters are as follows:
$ sudo chgrp -Rh www-data /var/www/html/nextcloud
In this tutorial, we introduced you
chgrp Commands in Linux. Although the chown command can be used to achieve the same function, the chgrp command provides a simpler method.
Someone may ask why we have chgrp when the chown command exists. The Chgrp code was built earlier, and chown was initially unable to set up groups.
Note: Only change the group
chown :group It is still not portable or standard on many platforms.
Thank you for reading, and please provide your feedback on the comment section below.