A Linux terminal window on a laptop
Fatmawati Achmad Zaenuri/Shutterstock.com

The chgrp command on Linux changes the group ownership of a file or directory. Why use it instead of chown ? Sometimes a Swiss Army knife is great, but when you really need a scalpel, only a scalpel will do.

When You Should Use chgrp

You use the chgrp command to change the group ownership of a file or directory. The chown command allows you to change the user owner and the group owner of a file or directory. So why would you need or use chgrp?

Well, for one thing, it’s easy. Using chown to change just the group owner setting is a little counterintuitive. You have to be very careful with the syntax. It hinges on the correct placing of a colon “:”. Misplace that, and you’re not making the change you thought you were.

The syntax of chgrp is altogether more straightforward. It also has a neat feature that tells you in simple terms what changes it has just made.

It is a purpose-built and dedicated tool for the task at hand. chgrp completely embraces the Unix design principle of doing one thing and doing it well. Let’s see what it has to offer.

Changing the Group Ownership of a File

To change the group ownership of a file is very straightforward. You must use sudo with chgrp. Groups are not owned by users, so whether a file or directory is moved from one group to another is not a decision that sits with the average user. That’s a job for someone with root privileges.

We’re going to change the group ownership of a C source file called “gc.c.” We’re going to change it to the “devteam” group.

We can check the current ownership values by using ls with the -l (long listing) option.

ls -l

This is the command to change the group ownership. Type sudo, a space, chgrp , a space, the name of the group we’re going to set as the group owner, a space, and the name of the file.

sudo chgrp devteam gc.c

We’ll check that the change has been made by using ls -l again.

ls -l

sudo chgrp devteam gc.c in a terminal window

We can see that the group ownership has been changed from “dave” to “devteam.”

To change the group ownership for a set of files all at once, you can use wildcards. Let’s change the group ownership for all of the C source files in the current directory. We will use this command:

sudo chgrp devteam *.c

We can check the change has been made as we expected by using ls -l.

ls -l

sudo chgrp devteam *.c ina terminal window

All of the C source files in this directory have been changed so that their group ownership is “devteam.”

By using the -c (changes) option chgrp will list the changes it has made to each file. Suppose we made a mistake, we wanted the C source files to have their group ownership set to “researchlab.” Let’s correct that now. We’ll use this command:

sudo chgrp -c researchlab *.c

sudo chgrp -c researchlab *.c in a terminal window

The changes are made for us, and each one is listed as it happens, allowing us to verify that what we have changed is correct.

Changing the Group Ownership of a Directory

Changing the group ownership of a directory is just as simple. We can use this command to change the group ownership for the directory “backup.”

sudo chgrp -c devteam ./backup

To be clear, this command will change the group ownership of the directory itself, not the files inside the directory. We will use ls -l with the -d (directory) option to verify that this is the case.

ls -l -d

sudo chgrp -c devteam ./backup in a terminal window

The group ownership of the directory itself has been changed to “devteam.”

The Recursive Option

If we want to change the group ownership for the files and directories stored within a directory, we can use the -R (recursive) option. This will cause chgrp to change the group ownership for all files and subdirectories below the target directory.

Let’s try this with the “backup” directory. Here is the command:

sudo chgrp -R devteam ./backup

We’ll review the files in one of the nested subdirectories with the ls command, and we’ll also check the settings of one of the nested subdirectories by using ls .

ls -l ./backup/images
ls -l -d ./backup/images

sudo chgrp -R devteam ./backup in a terminal window

We can see that the group ownership has been changed both for the files in the nested subdirectories and for the nested subdirectories.

Using a Reference File

So far, we’ve been explicitly telling chgrp the name of the group we wish to use. We can also use chgrp in way that says “set the group ownership of this file to the same group ownership as that file.”

Let’s set the group ownership of “gc.h” to be the same as “gc.c.”

We can check the current settings of “gc.c” and “gc.h” using ls.

ls -l gc.c
ls -l gc.h

The option we need to use is the --reference option. The group ownership is copied from the reference file to the other file. Take care to get the files the right way round.

sudo chgrp --reference=gc.c gc.h

We’ll use ls to check the settings have been transferred to “gc.h.”

ls -l gc.h

sudo chgrp --reference=gc.c gc.h in a terminal window

The file “gc.h” now has the same group ownership as “gc.c.”

Using chgrp with Symbolic Links

We can use chgrp to change the group ownership of symbolic links, or of the file pointed to by the symbolic link.

For this example, we’ve created a symbolic link called “button_link.” This points to a file called “./backup/images/button_about.png.” To change the group ownership of the file, we must use the --dereference option. This will change the settings for the file and leave the symbolic link unchanged.

Let’s check the settings for the symbolic link using ls -l.

ls -l button_link

The command to change the file is:

sudo chgrp --dereference devteam button_link

We’ll check that the symbolic link is unchanged using ls, and we’ll also check the group ownership settings for the file.

ls -l button_link
ls -l ./backup/images/button_about.png

sudo chgrp --dereference devteam button_link in a terminal window

The symbolic link is unchanged, and the “button_about.png” file has had its group ownership modified.

To change the group ownership of the symbolic link itself, we must use the --no-dereference option.

The command to use is:

sudo chgrp --no-dereference devteam button_link

We’ll use ls -l to verify that the new group ownership has been set for the symbolic link.

ls -l button-link

sudo chgrp --no-dereference devteam button_link in a terminal window

This time the affected item was the symbolic link itself, not the file it points to.

Nice and Simple

Simple is good. It means that there is less to remember and less to get confused with. That should mean fewer mistakes.





Source link