In Linux, the “chgrp” command is utilized to change the ownership of any file/directory group. The group is either the owner group or the supplementary group. In most cases, the “group” name is the same as the file’s owner name.
This post aims to illustrate the purpose, working, and functionalities of the “chgrp” command in Linux. The guideline of this post is defined below:
Let’s discuss the “chgrp” command in detail.
What is the chgrp Command in Linux?
The core functionality of the “chgrp” command is based on the syntax followed by it, which is given below:
Syntax:
$ chgrp [OPTION]... GROUP FILE...
The syntax contains the following components:
- chgrp: Represents the “chgrp” command.
- Option: Supported options of the “chgrp” command.
- Group: Group name of the specific file or directory
- File: target file whose group ownership needs to be changed.
The list of options supported by this command can be obtained as follows.
$ chgrp --help
How to Use chgrp Command in Linux?
The main objective of the “chgrp” command is to modify the group name of the specific file or directory. To do so, the user must have the root privileges before changing the group ownership of a file and directory.
Let’s practically implement the “chgrp” command.
Example 1: Change the Group of Files
The group ownership of the particular file or directory can be checked using the Linux “ls -l” command. Let’s implement how single or multiple files group can be changed:
Changing Group of a Single File
In this example, the “ls -l” command is displaying the group name of “file1” that is “itslinuxfoss”:
$ ls -l
Now, use the “chgrp” command with the new group name of the “file1.txt” to change its existing group:
$ sudo chgrp milton file1.txt
The “file1.txt” group has been changed from “itslinuxfoss” to “milton”.
Changing Group of Multiple Files
The user can also change the multiple files group name by following the syntax below:
$ sudo chgrp [group_name] [file1]......[fileN]
Suppose the “Doc” directory contains three files having group “itslinuxfoss” as shown in the screenshot via the “ls -l” command:
$ ls -l
Execute the below command to change the group of the above-highlighted files:
$ sudo chgrp johnson File1.txt File2.txt File3.txt
It is verified that the “File1.txt”, “File2.txt”, and “File3.txt” group has been changed from “itslinuxfoss” to “johnson”.
Example 2: Change the Group of a Directory Recursively
The “chgrp” command only changes the directory group name by default. However, It does not modify its files and subdirectories’ group name. The “chgrp” command provides the “-R” option for changing group ownership of all the available subdirectories and files in the specific directory:
$ sudo chgrp -R anna Notes
The “Notes” directory contains three files. The above output verifies that all the files’ group name is now “anna”.
Example 3: Change the Group of a File Using the Reference File
The user can change the ownership of a file or a directory by referencing a file to the target file. Suppose the “file2.txt” has the member of the group “anna” as shown below:
$ ls -l
Now reference this file to the target file “file3.txt” for changing the group ownership and apply the “file2.txt” permissions to the “file3.txt”:
$ sudo chgrp --referernce=file2.txt file3.txt
The “file3.txt” group ownership has been changed to “anna”
Example 4: Display the chgrp Command Detail
The “-c” option is helpful to display the execution details of the “chgrp” command. Run the “chgrp” command with the combination of “-c” and “-R” options to recursively list down the details of a particular “Notes” directory:
$ sudo chgrp -c -R milton Notes
Example 5: Hide the Errors
Some errors occur while changing the group name of a file or directory i.e “no such file or directory exists”. The “chgrp” command provides the “-f” option to hide any type of error while updating the group ownership:
$ sudo chgrp johnson extrafile.txt
Now, run the below-mentioned command to hide the above error:
$ sudo chgrp -f johnson extrafile.txt
The error is hidden now.
That’s all about the “chgrp” command.
Conclusion
In Linux, the “chgrp” stands for “change group”. Changing the ownership of a group associated with a particular file or directory is useful. The general syntax of the “chgrp” command is “$ chgrp [OPTION]… GROUP FILE…”. It is generally available in most Linux distributions. It supports a lot of options to get the desired output during changes. This post fully demonstrates the goal, working, and functionalities of the “chgrp” command in Linux.