Linux Chgrp Command for Beginners (5 Examples)
On this page
- Linux chgrp command
- Q1. How to change group of file/directory using chgrp
- Q2. How chgrp deals with symbolic links
- Q3. How to make chgrp pick group information from a reference file
- Q4. How to ask chgrp to make changes recursively
- Q5. Why use chgrp when chown can also be used to change groups
Here at HowtoForge, we recently discussed the chown command which lets users change the owner as well as group of file (or a directory) in Linux. But did you know there exists a dedicated command-line utility that you can use when it comes to changing group-related information? The tool in question is chgrp, and in this tutorial, we will be discussing this tool using easy to understand examples.
But before we do that, it's worth mentioning that all examples and instructions mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
Linux chgrp command
As you'd have already understood by now, if the requirement is to only change the group of a file or directory, then you can use chgrp instead of chown. The tool provides several command line options that you can use in different situations. Here's the generic syntax of chgrp:
chgrp [OPTION]... GROUP FILE...
The following Q&A-type examples should make the chgrp command more clear.
Q1. How to change group of file/directory using chgrp
The basic usage of the chgrp command is very easy. Suppose you want to change the group of a file 'file1' whose current group is 'himanshu'. The requirement is to change the group ownership to 'root.' Here's the command we need to run in this case:
chgrp root file1
Depending upon your current level of privileges, you may want to run the above command with 'sudo' (not sure what 'sudo' is, read this).
The above screenshot confirms that the group ownership was successfully changed to 'root.'
Q2. How chgrp deals with symbolic links
By default, the chgrp command affects the referent of a symbolic link. This means that any change made through the tool doesn't get applied to the symbolic link, but to the file it's referring to instead. For example, consider the symbolic link 'file1symlink' which links to 'file1'.
Both 'file1' and 'file1symlink' have their owner and group set to 'himanshu'.
Now, if you try changing the group of the symbolic link file, you'll see that 'file1symlink' will remain unaffected, but group ownership of 'file1' gets changed instead.
sudo chgrp root file1symlink
However, if you want, you can change this behavior by using the -h command-line option.
sudo chgrp -h root file1symlink
The following screenshot shows the aforementioned command changed the group of the symbolic link this time:
Q3. How to make chgrp pick group information from a reference file
If you want, you can ask the chgrp command to pick group information from a file, saving you the effort of manually specifying group name on the command line. This feature can be accessed through the --reference command-line option, which requires you to specify the name of the reference file.
chgrp --reference=[ref-file-name] FILENAME
chgrp --reference=file2 file1
The aforementioned command will make the group ownership of file1 same as that of file2.
Q4. How to ask chgrp to make changes recursively
In case - while dealing with directories and subdirectories - you want to make recursive changes, you can do so using the -R command-line option.
chgrp -R GROUPNAME DIRECTORY-OR-PATH
Q5. Why use chgrp when chown can also be used to change groups
One could argue that if the chown command can also be used to tweak group-related information, then why chgrp is required in the first place? Well, firstly, chgrp is simple to use compared to chown when all you need to do is to change the group of a file/directory. And secondly, since chown is also capable of doing other stuff, one would not want to accidentally make changes while working on, say, a production server.
Chgrp isn't a difficult command to understand and use. We've covered almost all its major options/features here. Try the examples we've mentioned in this tutorial, and you should be ready to use the command. In case of any doubt or query, head to the tool's man page.