In Linux, each file and directory is associated with a user and group owner to decide which user and group can read, write and execute the file and directory. The chgrp command is used to change the group ownership of files and directories on Linux. Unlike the chown command, the chgrp command just requires you to specify a group name.
In this post, we will show you how to use the chgrp command in Linux for various system administration situations.
Basic Syntax of chgrp Command
The basic syntax of the chgrp command is shown below:
chgrp [option] groupname file/directory
A brief explanation of each option is shown below:
- -c Display verbose output only after the change is made.
- -f Suppress most error messages
- -v Show diagnostic for every processed file.
- -R Apply ownership on files and directories recursively.
- –help Used to display help information.
Display Group Ownership Of a File
Before changing the group ownership of any file and directory, it is important to know how to show the existing group ownership of any file and directory.
You can use the ls -l command to display the exiting group ownership of any file and directory:
ls -l hitesh.txt
This will display the group ownership of the file hitesh.txt, as shown below:
-rw-rw-r-- 1 root vyom 0 Mar 15 09:48 hitesh.txt
As you can see, vyom is the group owner of the file hitesh.txt.
Also Read
How to Use chown (Change Ownership) Command in Linux
Change Group Ownership Of a Directory
You can use the following syntax to change the group ownership of the directory:
chgrp groupname directory-name
For example, to change the group ownership of the directory /var/www to www-data group, run the following command:
chgrp www-data /var/www
You can use the -R flag with the chgrp command to change the group ownership of the specified directory including all sub-directories:
chgrp -R www-data /var/www
Change Group Ownership Of a File
Use the following syntax to change the group ownership of a file:
chgrp groupname file-name
For example, to change the group ownership of the file hitesh.txt to sudo group, run the following command:
chgrp sudo hitesh.txt
If you want to change the group ownership of multiple files, run the following command:
chgrp sudo file1 file2 file3
Copy Group Ownership From a Reference File
The chgrp command also allows you to copy the group ownership information from a reference file. The basic syntax to copy the group ownership is shown below:
chgrp [OPTION] --reference=[RFILE_NAME] [FILE_NAME]
For example, to copy the group ownership from the file named hitesh.txt to vyom.txt, run the following command:
chgrp --reference=hitesh.txt vyom.txt
Also Read
How to Change Open File Limit in Linux
Display chgrp Execution Information
You can use the chgrp command with the -c option to display a list of changes chgrp has made to each file specified.
For example, to see the changes that happened in the /var/www/html directory, run the following command:
chgrp -c -R sudo /var/www/html
You should see all changes made by chgrp command in the following output:
changed group of '/var/www/html/index.html' from root to sudo changed group of '/var/www/html/index.nginx-debian.html' from root to sudo changed group of '/var/www/html/example.com/index.html' from root to sudo changed group of '/var/www/html/example.com' from www-data to sudo changed group of '/var/www/html' from root to sudo
Conclusion
In this post, we explained how to use the chgrp command to change the group ownership of a file or directory. We also used some options with the chgrp command to customize the process. Hopefully, this post will help you in a multi-user environment. Try it on dedicated hosting from Atlantic.Net!