The chgrp command in Linux can change the group ownership of one or multiple files or directories. In Linux, every file has a few permissions: read, write, and execute. These permissions are assigned to specific users and groups to allow access to these operations
This tutorial will show you a few ways you can utilize the chgrpcommand in your Linux terminal for various system administration situations. We’ll show you some of the most frequent and common options used with this command and how to apply them.
Privileged access to your Linux system as root or via the sudo command.
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The chgrp command changes the group ownership of a file or directory. There are other similar commands to the chgrp command such as the chown command; the chown command changes the user ownership of a file instead of the group ownership. A user can only change the permissions of a file with the chgrp command if they have either the user permissions or the group permissions to do so.
You can always check the group permissions of any file or directory in Linux by using the ls -l command.
chgrp command in Linux Basic Examples
Use the syntax below to use the chgrp command to change the group permissions of a specified file. In this example, we’ll change the file’s group to sudo, which is a common default group available on most systems.
Using the -v option, we can get the chgrp command to tell us what it is doing in the background so we can see it in action. In the example below, we’ll show you how to use the syntax in your Linux terminal to get this output.
The chgrp command can also change the group ownership for multiple files and directories at once. We can achieve this by typing more than one file or directory:
The most advanced usage of the chgrp command would be when you need to change group ownership of an entire directory structure. Check out the commands below to see how to recursively set group ownership on a bunch of files simultaneously.
chgrp command in Linux Advanced Examples
Use the -R option with the chgrp command to change the group ownership of an entire directory’s contents.
We’ve included the -v option in the screenshot above, so you can see that the -r option is recursively changing the group ownership of all files and subdirectories in the Downloads directory.
You can use the chgrp command to change the group ownership of a symbolic link as well. Symbolic links are used as a kind of “shortcut” for accessing files or directories. By default, chgrp will follow the symbolic link and change group ownership for the file it’s pointing to. You can also use the -R option on symbolic links.
$ chgrp -R sudo symboliclink01
Closing Thoughts
In this tutorial, we learned all about the chgrp command on Linux which is important to master for personal use and system administration to change the group ownership of files and directories. It’s especially convenient for recursively changing the group ownership of a file or directory by appending the -R option.