Updated April 14, 2023
Introduction to Linux Chgrp
In the Linux operating system, each file or directory is associated with the owner and the group with the respective access type. The chgrp command is used to change the group ownership of the directory or the file. We are having one more way to change the file ownership i.e. “chown”. For changing the file owner, we can use the “chown” command. For changing the group ownership of the file, we can use the “chgrp” command. In this topic, we are going to learn about Linux Chgrp. The chgrp utility is written by David MacKenzie and Jim Meyering. Note: If we don’t know the valid group or you want to create a group. We should have administrator or sudo privileges. Accordingly, we can create a new group and change the group ownership as per the newly created group.
Syntax of Chgrp Command
chgrp [OPTION]... [ GROUP NAME ][ FILE ] ...
chgrp [OPTION]... –reference=RFILE FILE...
- chgrp: We can use the “chgrp” keyword in the syntax or command. It will take the three arguments like the option, GROUP Name, and file name. As per the provided arguments, it changes the group ownership of the file or directory.
- OPTION: We can provide the different flags as options that are compatible with the “chgrp” command.
- GROUP: We can provide the GROUP name to the “chgrp” command.
- FILE: We need to keep the file name or directory name in the “chgrp” command.
How does Linux Chgrp Command work?
In Linux, we are having different ways to change the owner and group ownership of files and directories. We can use the “chgrp” command to change the group ownership of the file as well as the directory. The “chgrp” command will accept the different arguments like the option, group name, and the file or directory (name or path). As per the giving input, the “chgrp” command will change the group ownership of the file with the respective group name. Below are the lists of “chgrp” command options,
Sr No | Option | Description |
1 | -c, –changes | It will print the verbose message but only when it will report the changes is made |
2 | -f, –silent, –quiet | It will suppress most error messages |
3 | -v, –verbose | It will print the output of diagnostic while processing every file |
4 | –dereference | This is the default nature; It will affect the referent of each symbolic link rather than the symbolic link. |
5 | -h, –no-dereference | It will affect the symbolic links instead of any referenced file |
6 | –no-preserve-root | It will not treat ‘/’ as special (the default). |
7 | –preserve-root | It is useful to fail to operate recursively on ‘/’ |
8 | –reference=RFILE | It will use the RFILE’s group rather than a GROUP value |
9 | -R, –recursive | It will help to operate on files and directories recursively in nature. |
Examples to implement Linux Chgrp command
Here are the following examples mentioned below.
Example#1 – Change Group Ownership of File
The “chgrp” is a very simple and common way to change the group ownership of the filein a Linux environment.
Command :
chgrp edu file1.txt
Explanation:
As per the below screenshot 1 (a), we are having the “file1.txt”. In screenshot 1 (b), we are changing the group ownership from “root” group to the “edu” group.
Output :
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Change Group Ownership of Directory
By default, the group ownership of the newly created directory is the same as the owner only. But we can change the group ownership of the directory as well.
Command:
chgrp edu data
Explanation:
As per the above chgrp command, we can change the group ownership from root to edu group of the “data” directory.
Output :
Screenshot 2 (a)
Screenshot 2 (b)
Example #3 – Change Group OwnershipRecursively
In the chgrp command, we are the functionality to change the group ownership of the file or folder recursively. There is no need to change the group ownership individually.
Command :
chgrp -R edu data
Explanation:
As per the above command, we are changing the group ownership of the number of files from the “root” group to the “edu” group.
Output :
Screenshot 3 (a)
Screenshot 3 (b)
Example #4 – Change Group Ownership with File Reference
In the Linux environment, we are having the functionality to change the file or directory ownership with the help of file reference. Note: While providing the file reference, make sure that the associated group of the reference file will be available in the Linux environment.
Command :
chgrp --reference=file1.txt test_file.txt
Explanation:
As per the above command, we are using the “file1.txt” file as a group reference to change the group ownership from root to edu.
Output :
Screenshot 4 (a)
Screenshot 4 (b)
Example #5 – With “-f” Option
In the Linux ecosystem, we are having the functionality to suppress the chgrp error message. To suppress the error messages we need to use the “-f” option with chgrp command.
Command :
chgrp -f edu abc.txt
Explanation:
As per the above command, we suppress the error messages that are coming while working with the “chgrp” command. We have used the “-f” option in the above command.
Output :
Screenshot 5 (a)
Screenshot 5 (b)
Example #6 – With “-v” Option
In the chgrp command, we can print all the non-activity as well as the activity which is going on the very file or folder.
Command :
chgrp -v edu file2.txt
Explanation:
In the above “chgrp” command, we have used the “-v” option. It will print all the information while doing any group ownership changes. It will work on the file as well as on the directory level.
Output :
Conclusion
We have seen the uncut concept of “Linux Chgrp command” with the proper example, explanation, and command with different outputs. The chgrp command is used to change the group ownership of the file and folder. The major advantage of the chgrp command is file reference.
Recommended Articles
We hope that this EDUCBA information on “Linux Chgrp” was beneficial to you. You can view EDUCBA’s recommended articles for more information.