How to use the chgrp command on Linux
When to use chgrp
You use chgrp command to change
group ownership of a file or directory. The chown command allows you to
change the user's owner and the group owner of a file or
directory. So why should you need or use chgrp?
For one thing, it's simple. Using chown only the group owner
setting is slightly opposite. You have to be very careful with
syntax. It is related to the correct placement of a colon
":". Make it wrong and you will not make the change you thought
you were.
The syntax of chgrp is quite
straightforward. It also has a neat feature that tells you in simple words
what changes it has just made.
It is a specially built and dedicated tool for
the task. Chgrp completely embraces the Unix design principle
of doing one thing and doing it well. Let's see what it has to offer.
Change group ownership to a file
Changing group ownership to a file is very
easy. You have to 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 for the average user. It's a job for someone with root
privileges.
We are going to change the group ownership to
a C source file called "gc.c.". We'll change it to the
"devteam" group.
We can check the current owner values
using lsthe -l(long list) option.
ls -l
This is the command to change group
ownership. Type sudo, a room chgrp,, a space, the name
of the group we want to specify as the group owner, a space and the name of the
file.
sudo chgrp devteam
gc.c
We check that the change has been made by
using ls -lagain.
ls -l
We can see that the
ownership in the group has changed from "dave" to
"devteam."
If you want to change
group ownership of a set of files at once, you can use wildcards. Let's
change group ownership for all the C source files in the current
directory. We use this command:
sudo chgrp devteam *.c
We can check that the
change has been made as we expected by using ls -l.
ls -l
All source files in
this directory have been modified so that the group ownership is
"devteam."
Using the -c(changes) option chgrp will display 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 fix that now. We use this command:
sudo chgrp -c
researchlab *.c
The changes are made
for us, and each one is listed as it happens, so that we can confirm that what
we have changed is correct.
Change group ownership to a directory
Changing group
ownership of a directory is just as easy. We can use this command to
change group ownership of the "backup" directory.
sudo chgrp -c devteam
./backup
To be clear, this
command will change the group ownership of the directory itself, not the files
in the directory. We will use ls
-lwith -d(catalog) to confirm
that this is the case.
ls -l -d
The group ownership of
the directory itself has been changed to "devteam."
The recursive option
If we want to change
group ownership of the files and directories stored in a directory, we can use
the -R(recursive) option. This will lead chgrp to changing the group
ownership of all files and subdirectories under the target directory.
Let's try this with
the "backup" directory. Here is the command:
sudo chgrp -R devteam
./backup
We will review files in
one of the nested subdirectories with the ls command, and we also
check the settings of one of the nested subdirectories using ls.
ls -l ./backup/images
ls -l -d
./backup/images
We can see that the
group ownership has changed both for the files in the nested subdirectories and
for the nested subdirectories.
Uses a reference file
So far we have
explicitly stated the chgrp name of the group we want to use. We can
also use chgrp the way that says "set group ownership
to this file to the same group ownership as that file."
Let us set the group
ownership of "gc.h" to be the same as "gc.c."
We can check the
current settings for “gc.c” and “gc.h” by using ls.
ls -l gc.c
ls -l gc.h
The alternative we
need to use is –reference alternative. The group ownership is
copied from the reference file to the
other file. Make sure you get the files the right way.
sudo chgrp
--reference=gc.c gc.h
We use it ls to check that the
settings have been transferred to "gc.h."
ls -l gc.h
The file “gc.h” now
has the same group ownership as “gc.c.”
Using chgrp with symbolic links
We can use chgrp to change group
ownership to symbolic links, or the file that symbolic link refers to.
For this example, we
have created a symbolic link called "button_link." This points
to a file named "./backup/images/button_about.png." To change
group ownership of the file, we must use the –dereference option. This
will change the settings of 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 check that the
symbolic link is unchanged using ls, and we will also check the group owner
settings for the file.
ls -l button_link
ls -l
./backup/images/button_about.png
The symbolic link is
unchanged, and the "button_about.png" file has had its group ownership
changed.
To change group
ownership of the symbolic link itself, we must use the --no-dereference alternative.
The command you use
is:
sudo chgrp
--no-dereference devteam button_link
We use it ls -lto confirm that the
new group ownership is specified for the symbolic link.
ls -l button-link
This time, the
affected element was the actual symbol link, not the file it points to.
Simple is
good. This means that there is less to remember and less to be confused
with. That should mean fewer mistakes.
No comments:
Post a Comment