chgrp command in Linux with examples

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 chgrp command 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.

In this tutorial you will learn:

  • How to use the chgrp command on Linux

"<yoastmark

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software chgrp
Other 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

  1. 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.
    $ chgrp sudo file01
    

    "Using

  2. 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.
    $ chgrp -v sudo file02
    

    "Using

  3. 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:
    $ chgrp sudo file01 file02
    

    Use chgrp command to change the group ownership of file01 and file02
    Change the group ownership of file01 and file02
NOTE
You can always use the man command to read more about the chgrp command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

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

  1. Use the -R option with the chgrp command to change the group ownership of an entire directory’s contents.
    $ chgrp -R sudo /home/linuxconfig/Downloads
    
    Recursively setting group ownership
    Recursively setting group ownership

    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.

  2. 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.