chgrp Command Examples

If you used our chmod calculator, 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

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:

chgrp group_name file_name

You can also change the group for multiple files at once:

chgrp group_name file1 file2 file3

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.

💡
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 chgrp with a directory, it only changes the group of the directory. The files and sub-directories remain the same.

If you want to change the group of all the files in the directories and in the sub-directories, you can use the recursive option -R.

chgrp -R group_name path_to_directory

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 provides a verbose mode that tells you what operations your chgrp command performed. You can use the option -v to run chgrp command in verbose mode.

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.

💡
You can use chgrp to give execute permission to a command (in bin or in init) or so that the command can be executed by all users belonging to a specific group (instead of root).

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, chgrp provides a dedicated way of changing the group based on a reference file instead of using the name of the group explicitly.

chgrp --reference=file2 file1

This is particularly helpful if you are writing a script where the group owners of files need to be changed as a reference file.

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 chown command for changing the group of a file but changing only the group through chown command is not standard. Also, it could be confusing to use chown for this purpose. chgrp is pretty straightforward and it is recommended to use chgrp command for changing the group of a file or directory.

I hope you liked the chgrp command examples. If you have questions or suggestions or a simple thanks, please use the comment box below.