HomeA-Z CommandsHow To Use "chgrp Command" (Change Group) in Linux

How To Use “chgrp Command” (Change Group) in Linux

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 know that the chgrp, chmod, and chown commands are the most powerful and used terminal commands.

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 handy and a life savior in critical situations.

Why chgrp, When chown Can Do The Same?


You might have been wondering why you need to use the change group command when you can change the entire ownership with a chown command.

Here is the answer to why, no doubt, the chown command is a 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 file or a group of files. If you’re the project owner and want to hand over your files or 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, and add more members to the main or supplementary groups.

This post will show the most common and most used chgrp commands on Linux.

1. Check the Group Info with chgrp


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

ls -l

chgrp Command ls command

2. chgrp Command Syntax


The syntax of the change group command on Linux is easy and hassle-free. This cross-platform terminal command can be executed on all major Linux desktops, servers, and Unix-like operating systems.

Here is the basic syntax pattern of the chgrp command.

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

sudo chgrp command ubuntupitdemo test

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. 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 a file, folder, or directory ownership, 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.

sudo mkdir New_Dir
sudo chgrp ubuntupitdemo New_Dir

new directory group chnage

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 you need to change for 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 refer or match 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 in 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 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 to 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 combining the chown and chgrp in the same command, that is not impossible either. In a way, you can first add the chown command and then define which user group you want to assign for that command.

The command below might help you 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 chown
man chgrp

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 servers, 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 defines which user group can execute which commands. You can find the sudoers files inside the /etc/sudoers directory. 

Insights!


The change group command makes your Linux 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.

Proper knowledge of the change group command 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 it with your friends and the Linux community. You can also write 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


You May Like It!

Trending Now