14.4 C
New York
Friday, June 9, 2023
HomeLinuxA-Z CommandsLearn Usage of chgrp Command (Change Group) in Linux System

Learn Usage of chgrp Command (Change Group) in Linux System

In Linux, the change group or the chgrp command is mostly used to change the user group of the system. The chgrp command works for both Linux and other Unix-like operating systems. If you’re a system admin, you must already know that the chgrp, chmod, and chown commands are the most powerful and most used terminal commands.

Conventionally, the chgrp command is run with the sudo or root privilege in the system, but if you’re the owner of that particular file or directory, you can execute the change group command as well. The most common usages of the change group command are installing a new package in the group, moving one file to another directory from the user group, and editing a non-read/writable file. This chgrp command can be very much handy and a life savior in critical situations.

Why chgrp, When chown Can Do The Same?


Probably, you might have been wondering why you need to use the change group command while you can just change the entire ownership with a chown command? Here is the answer why; no doubt, the chown command is a very powerful command. Sometimes, the chown command can make changes that are unwanted and are not acceptable. To protect your files from careless and accidental use of the chown command, it’s safe to use the chgrp command.

chgrp (Change Group) Command in Linux


The chgrp command is widely used to change the ownership of a single or a group of a file. If you’re the project owner and now you want to hand over your files, or you want to add a new team member to your team without changing the entire directory, the change group command might be very helpful for you. 

Even if you’re not the main owner of the file, but you belong to the group and have sudo access, you can add members, change the group permission, add more members to the main or supplementary groups. In this post, we will see the most common and most used chgrp commands on Linux.

1. Check the Group Info with chgrp


At the very beginning of the chgrp command, you might want to know the group name to ensure that you’re in the group. To do that, you can simply execute the ls -l command on your terminal shell. In return, you will get to see the name of the user, group, and file details.  

chgrp Command ls command

ls -l

2. chgrp Command Syntax


The syntax of the change group command on Linux is very easy and hassle-free to understand. This cross-platform terminal command can be executed on all major Linux desktop, server, and Unix-like operating systems. Here is the basic syntax pattern of the chgrp command. 

sudo chgrp command ubuntupitdemo test

chgrp [OPTION]… GROUP path/FILE_NAME
sudo addgroup ubuntupitdemo
sudo chgrp ubuntupitdemo test.txt
ls -l

3. Add User To The Group 


As I already mentioned, you can add more people to your working group or the directory group if you have sudo access. Here is the command that you can execute on the terminal shell. Here, for instance, I’ve added the user ubuntupitdemo in the user group. 

$ sudo useradd ubuntupitdemo
# touch test.txt
# ls -lt test.txt

4. chgrp Command Ownership


If you need to change the ownership of a file, folder, or directory, you can easily execute the following chgrp command on your shell. Here, for instance, I’m going to create a new directory, and I will change the group of this directory. 

new directory group chnage

sudo mkdir New_Dir
sudo chgrp ubuntupitdemo New_Dir

5. Recursively Change the Group Privilege


In Linux, you can alter the ownership of a directory through the change group command. The following chgrp command with a -R flag will allow you to define the elements inside the group that exactly you need to change of that active set of the directory. 

sudo chgrp -R ubuntupitdemo New_Dir

6. chgrp Command with the Reference File


If you need to use a previous reference or by referring or matching with another group, you can use the following change group command on your shell. Here, you will need to add the -R flag with the reference syntax. 

sudo chgrp -R --reference=test.txt New_Dir

In Linux, there are many operations and folders inside the filesystem, where the soft link of a directory is used to indicate the original directory. However, if you need to change the group of that folder, you can use the –dereference/ and –no-dereference syntax with the change group command. 

sudo chgrp --dereference ubuntupitdemo symbolic_link

While you use the -dereference syntax, it will change the original name of that group. On the other hand, the no-dereference syntax will only change the name of the symbolic link or the soft link while keeping the original folder unchanged. 

8. Display chgrp Execution Details


For system admins and Linux power users, this command will be helpful. If you want to see the log of changes of a file on your system, please execute the mentioned command. 

sudo chgrp -c -R [GROUP_NAME] [DIRECTORY/FILE_NAME]
sudo chgrp -c -R ubuntupitdemo example

9. Hide chgrp Command Errors


If you only need to show the final output of a command and you need to hide all the errors from the terminal shell, the below-mentioned change group command with a -f flag will hide all the errors. 

sudo chgrp -f [GROUP_NAME] [DIRECTORY/FILE_NAME]

10. Add Multiple Files the chgrp Command


If you need to change the group of multiple files at a time, you can use the below-mentioned change group command as an array argument in the shell. 

sudo chgrp www-data file1 file2 dir1

11. Run chown And chgrp Command Together 


If you’re thinking of using the chown and the chgrp combined in the same command, that is not impossible either. In a way, you can first add the chown command then you can define which user group you want to assign for that command. The below-provided command might help you to understand commands and syntaxes. 

$ chown someusername:somegroupname filename.ext
$ sudo chown user_name:group _name file_name

You can go through the manual pages of the chown and chgrp command on your Linux system. 

man chgrp

man chown
man chgrp

Extra Tip: Issues You May Face on chgrp Command


Here, I will share the answers to some of the most asked questions about the change group command on Linux. Please check if you need to solve any of the following situations. 

If you face errors while executing the chgrp command on your Linux server or desktop, make sure that you’ve enclosed the directory path. In most cases, it does not cause any issue on the desktop, but be careful while executing the chgrp command on Linux server, Apache, Nginx, or SSH servers. 

If you need to reset the change group rules on your filesystem, you can use the setfacl command to restore the directory ownership. Both of the below-mentioned setfacl and getfacl commands are effective in restoring group ownership. 

setfacl --restore={dir}.facl
getfacl -R {dir} >{dir}.facl

In some cases, you might also face issues executing the chgrp command on Linux. In that case, you can edit the sudoers file on your system. The sudoers file is the controller that can define which user group is allowed to execute which commands. You can find the sudoers files inside the /etc/sudoers directory. 

Insights!


Using the change group command makes your Linux using experience more professional and powerful. It is very safe to execute if you know what you’re doing. Please be careful while executing the chgrp command to avoid the filesystem getting abandoned. With proper knowledge of the change group command, it can be the swiss army knife for a Linux user. In the entire post, we have seen the basic and powerful commands of the chgrp command on Linux. 

If you find this post useful and informative, please share this with your friends and the Linux community. You can also write down your opinions about this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Post

Must Read