chgrp Command on Linux changes group ownership of a file or directory. Why are you using it instead of?
chown ? Sometimes a Swiss Army Knife is great, but if you really need a scalpel, just a scalpel will do.
When to use
They use the
chgrp Command to change group ownership of a file or directory. the
chown The command allows you to change the user owner and group owner of a file or directory. So why should you need or use?
Well, for one, it’s easy. Using
chown Just changing the group owner’s mindset is a little counter-intuitive. You have to be very careful with the syntax. It depends on the correct placement of a colon “:”. If you embarrass this, you’re not making the change you thought you were.
The syntax of
chgrp is overall easier. It also has a nice feature that tells you in simple terms what changes have just been made.
It’s a specially designed and dedicated tool for the job at hand.
chgrp fully embraces the Unix design principle of doing one thing and doing it well. Let’s see what it has to offer.
Change group ownership of a file
Changing group ownership of a file is very easy. You have to use
chgrp. Groups do not belong to users. So whether a file or directory is moved from one group to another is not at the discretion of the average user. This is a job for someone with root privileges.
We are going to change group ownership of a C source file named “gc.c”. We’re going to change it to the “devteam” group.
We can check the current property values by
ls with the
-l (long listing) option.
This is the command to change group ownership. Type
sudo, a space,
chgrp , a space, the name of the group we will designate as the group owner, a space, and the name of the file.
sudo chgrp devteam gc.c
We’ll check if the change was made by using
ls -l again.
We can see that the group ownership has been changed from “dave” to “devteam”.
You can use wildcards to change group ownership for multiple files at once. Let’s change group ownership for all of the C source files in the current directory. We are going to use this command:
sudo chgrp devteam *.c
We can check that the change was made as expected by using
All of the C source files in this directory have been changed so that they are group owned by “devteam”.
By using the
-c (Changes) option
chgrp lists the changes it made to each file. Suppose we made a mistake, we wanted group ownership of the C source files to be set to researchlab. Now let’s correct that. We use this command:
sudo chgrp -c researchlab *.c
The changes are made for us and each one is listed as it happens so we can verify that the changes are correct.
Change group ownership of a directory
It is just as easy to change group ownership of a directory. With this command we can change the group ownership for the directory “backup”.
sudo chgrp -c devteam ./backup
To be clear, this command changes group ownership of the directory itself, not the files within the directory. We will use
ls -l with the
-d (Directory) option to check if this is the case.
ls -l -d
Group ownership of the directory itself has been changed to “devteam”.
The recursive option
If we want to change group ownership for the files and directories stored in a directory, we can
-R (recursive) option. This will cause
chgrp to change group ownership for all files and subdirectories under the target directory.
Let’s try the directory “backup”. Here is the command:
sudo chgrp -R devteam ./backup
We check the files in one of the nested subdirectories with the
ls Command, and we also check the settings of one of the nested subdirectories with
ls -l ./backup/images
ls -l -d ./backup/images
We can see that group ownership has changed for both the files in the nested subdirectories and the nested subdirectories.
Use a reference file
So far we’ve said it explicitly
chgrp the name of the group we want to use. We can also use
chgrp in the manner that says, “Set group ownership of this file to the same group ownership as this file.”
Let’s set the group ownership of “gc.h” to the same value as “gc.c”.
We can check the current settings of “gc.c” and “gc.h” with
ls -l gc.c
ls -l gc.h
The option we have to use is that
--reference Opportunity. The group ownership is copied from the reference file to the other file. Make sure you get the files the right way round.
sudo chgrp --reference=gc.c gc.h
ls to check whether 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 of symbolic links or the file pointed to by the symbolic link.
Therefore example, we made one symbolic link called “button_link”. This points to a file called “./backup/images/button_about.png”. To change group ownership of the file, we need to use the
--dereference Opportunity. This changes the settings for the file and the symbolic link remains unchanged.
Let’s check the settings for the symbolic link with
ls -l button_link
The command to change the file is:
sudo chgrp --dereference devteam button_link
We use ls to check that the symbolic link is unchanged, and we also check the group ownership settings for the file.
ls -l button_link
ls -l ./backup/images/button_about.png
The symbolic link is unchanged and the group property of the button_about.png file has been changed.
To change the group ownership of the symbolic link itself, we need to use the
The command to use is:
sudo chgrp --no-dereference devteam button_link
ls -l to verify that the new group ownership has been set for the symbolic link.
ls -l button-link
This time the element affected was the symbolic link itself, not the file it points to.
Nice and easy
Simple is good. It means there is less to remember and less to be confused with. That should mean fewer mistakes.
|Files||tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm|
|Processes||alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg|
|Networking||netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw|
Best Linux Laptops for Developers and Enthusiasts