chgrp command is used for changing the group of a file or directory in Linux. This guide shows you how to use chgrp command in Linux with practical examples.
What is chgrp command in Linux?
If you are aware of file permissions in Linux, you are probably aware of the group ownership.
chgrp command in Linux is used for changing the group of a file or directory. It stands for ‘change group’.
The syntax for the chgrp command is:
chgrp [options] groupname file>
5 Practical examples of chgrp command in Linux
Let’s see how to use chgrp command with these useful examples.
1. Change group of files/directories
This is the simplest and perhaps the most prominent use of the chgrp. To change the group ownership of a file or directory, you can use the chgrp command in the following manner:
You can also change the group for multiple files at once:
You don’t have to be in the same directory as the file. You can provide the absolute or relative path as well.
Your current privileges matter. If you try to change the groups to admin or root, you might need super user privileges. You should see a ‘operation not permitted’ error in such cases.
Tip: chgrp supports tab completion. Just type a few letters for the group name and hit tab to see what groups exist with those letters.
2. Use chgrp recursively to change group all the files and sub-directories
By default, if you use
If you want to change the group of all the files in the directories and
3. Know if you managed to change the group
You can figure out if the group has been changed by using the ‘ls -l’ command. But what if you changed the group for several files at once like using the recursive option you saw in the previous section?
chgrp -vR abhishek samplechanged group of 'sample/agatha.txt' from sudo to abhishek group of 'sample/a.text' retained as abhishek changed group of 'sample/text/sherlock.txt' from sudo to abhishek changed group of 'sample/text' from sudo to abhishek changed group of 'sample' from sudo to abhishek
You may notice that the verbose mode also tells if the group of a file remained the same. If you want to see this information only for the files for which there were actually a change in group ownership, you can use option -c.
Tip: You can use
4. Change the group
ownership the same as a reference file
Imagine that you want to change the group of file A the same as file B. How would you do that? You can look for the group of file B and then use the chgrp command with the group name of file B.
Well, that’s one way of doing it. However,
This is particularly helpful if you are writing a script where the group owners of files need to be changed as a reference file.
5. Using chgrp with symbolic links
By default, if you use the chgrp command with a symbolic link, it’s the group owner of the referenced file that gets changed while the group of symbolic link remains as it is.
For example, this is the sate of the link and its referenced file:
ls -l agatha.txt link.txt -r--r--rw- 1 abhishek abhishek 457 Aug 10 11:55 agatha.txt lrwxrwxrwx 1 abhishek abhishek 10 Aug 19 10:19 link.txt -> agatha.txt
Now if you change the group of the symbolic link like this:
chgrp sudo link.txt
The group of symbolic link will remain the same while the group of the referenced file will be changed.
ls -l agatha.txt link.txt -r--r--rw- 1 abhishek sudo 457 Aug 10 11:55 agatha.txt lrwxrwxrwx 1 abhishek abhishek 10 Aug 19 10:19 link.txt -> agatha.txt
If you want to change just the group ownership of the symbolic link and not the referenced file itself, you can use the -h option.
chgrp -h sudo link.txt
However, I won’t suggest it because in Linux, link permissions don’t have meaning. The referenced file is what matters here.
Why use chgrp when you can use chown for changing group?
You can also use
I hope you liked the chgrp command examples. If you have questions or suggestions or a simple thanks, please use the comment box below.