5 practical examples of chgrp command in Linux

Let’s see how to use the chgrp command with these helpful examples.

1. Change a group of files / directories

This is the simplest and perhaps most visible use of chgrp. To change group ownership of a file or directory, you can use the chgrp command as follows:

chgrp

You can also change the group for multiple files at the same time:

chgrp

You don’t need to be in the same directory as the file. You can also specify an absolute or relative path.

Your current privileges matter. If you try to change groups to admin or root, you may need superuser privileges. In such cases, you should see an “operation not permitted” error.

Hint chgrp supports tabs. Just enter a few letters for the group name and hit the tab to see which groups exist with those letters.

2. Recursively use chgrp to change the group of all files and subdirectories

By default, if you use the chgrp command with a directory, it only changes the group of the directory. Files and subdirectories remain unchanged.

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

chgrp -R

3. Know if you managed to change the group

You can determine if the group has been changed with the “ls -l” command. But what if you changed the group to multiple files at the same time using the recursive option you saw in the previous section?

the chgrp command provides a verbose mode that tells you what operations the chgrp command performed. You can use the -v option to run the chgrp command in verbose mode.

chgrp -vR abhishek samplechanged group of 'sample/andreyex.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 indicates whether the group of files remains the same. If you only want to see this information for files that actually have group ownership changes, you can use the -c option.

Tip You can use the chgrp command to grant permission to execute the command (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 ownership of the group to the same as the link to the file

Imagine that you want to change the group of file A in the same way as file B. How would you do this? You can find file group B and then use the chgrp command with the group name of file B.

Well, this is one way to do it. However, the chgrp command provides a dedicated way to change the group based on the referenced file instead of using the group name explicitly.

chgrp --reference=

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

5. Using chgrp with symbolic links

By default, if you use the chgrp command with a symlink, this is the group owner of the referenced file, which changes when the symlink group remains as it is.

For example, this is the state of a link and its associated file:

ls -l andreyex.txt link.txt 
-r--r--rw- 1 abhishek abhishek 457 Aug 20 10:26 andreyex.txt
lrwxrwxrwx 1 abhishek abhishek  10 Aug 21 11:35 link.txt -> andreyex.txt

Now if you change the group of the symbolic link like this:

chgrp sudo link.txt

The group of the symbolic link will remain unchanged while the group of the referenced file is changed.

ls -l andreyex.txt link.txt 
-r--r--rw- 1 abhishek sudo     457 Aug 20 10:26 andreyex.txt
lrwxrwxrwx 1 abhishek abhishek  10 Aug 21 11:35 link.txt -> andreyex.txt

If you only want to change the group’s ownership of the symbolic link and not the referenced file itself, you can use the -h option.

chgrp -h sudo link.txt

However, we won’t be suggesting this, because on Linux, connection permissions don’t make sense. This file matters.

Why use chgrp when you can use chown to change group?

You can also use the chown command to change a group of files, but changing only the group with the chown command is not standard. The chgrp command is fairly straightforward, and it is recommended that you use the chgrp command to change a group of files or directories.

Hope you enjoyed the chgrp command examples. If you have any questions or suggestions, or just thank you, please use the comment box below.

Related Posts