Open In App

chgrp command in Linux with Examples

Last Updated : 03 Nov, 2025
Comments
Improve
Suggest changes
7 Likes
Like
Report

The `chgrp` command in Linux is used to change the group ownership of a file or directory. All files in Linux belong to an owner and a group. You can set the owner by using “chown” command, and the group by the "chgrp" command.

Here is the example of Changing Group Ownership of a Single File

sudo chgrp geeksforgeeks abc.txt
For a single File
For a single File

Here the group name of the file abc.txt was changed from kcVirtual to geeksforgeeks. Note that when files are created the groupname of the file is the same as the owner under which the file was created.

Syntax of `chgrp` command in Linux

chgrp [OPTION]… GROUP FILE…chgrp [OPTION]… –reference=RFILE FILE…

Options available in `chgrp` command in Linux

First, we need to have administrator permission to add or delete groups. We can login as root for this purpose or use sudo. In order to add a new group, we can use:

sudo addgroup geeksforgeeks

1. `-c` or `--changes` Option

To describe the action for each File whose group actually changes.

Example:

sudo chgrp -c geeksforgeeks f1
-c
-c

2. `-f` Option

To suppress error messages.

Example:

sudo chgrp -f geeksforgeeks f2
-f
-f

3. `-v` Option

To describe the action or non-action taken for every File.

Example:

sudo chgrp -v geeksforgeeks f1
-v
-v

4. `--dereference` or `--no-dereference` Option

To change the group name of link files.

Example:

sudo chgrp --dereference geeksforgeeks symbolic_link
--dereference
--dereference

Here, symbolic_link is the link file for f1. When using the --dereference option, the group ownership of the actual file pointed to by symbolic_link gets changed.

Example:

sudo chgrp --no-dereference geeksforgeeks symbolic_link
file

Here, symbolic_link is the link file for f1. When using the --no-dereference option, the group ownership of the symbolic_link itself is changed, rather than the target file it points to.

Examples of chgrp Command

Example 1: Changing Group Ownership of Multiple Files

The 'chgrp' command can also handle multiple files at once. For instance:

chgrp developers file1.txt file2.txt file3.txt

Here, 'file1.txt', 'file2.txt', and 'file3.txt' will all be assigned to the 'developers' group.

Example 2: Changing Group Ownership of a Directory or Folder

To change the group ownership of a folder.

sudo chgrp geeksforgeeks GFG
For directory or folder
For directory or folder

Example 3: Recursively change the group ownership of a folder

To recursively change the group ownership of a folder and all of its contents.

sudo chgrp -R geeksforgeeks GFG
Recursively
Recursively
  • As we can see the group of the folder GFG and its contents F1, F2 was all kcvirtual initially and they were changed to geeksforgeeks with the single command.

Example 4: Using the groupname of a reference file

Using the groupname of a reference file to change the group of another file or folder.

sudo chgrp -R --reference=abc.txt GFG
reference file
reference file

The groupname of the reference file abc.txt was used to recursively change the group of the folder GFG and all its contents using the --reference option.

Suggested Quiz
5 Questions

Which command changes the group ownership of the file notes.txt to the group students?

  • A

    chgrp users notes.txt

  • B

    chgrp students notes.txt

  • C

    chown :students notes.txt

  • D

    chmod students notes.txt

Explanation:

chgrp group file changes only the group ownership.

Which command updates the group of multiple files (a.txt, b.txt) to devteam?

  • A

    chgrp devteam a.txt b.txt

  • B

    chgrp -R devteam a.txt b.txt

  • C

    chgrp -v a.txt b.txt devteam

  • D

    chgrp devteam *.txt

Explanation:

Passing multiple files directly after the group applies group ownership to all of them.

Which command ensures that a symbolic link itself, and not the file it points to, receives the updated group ownership engineers?

  • A

    chgrp --dereference engineers linkfile

  • B

    chgrp engineers linkfile

  • C

    chgrp --no-dereference engineers linkfile

  • D

    chgrp -R engineers linkfile

Explanation:

--no-dereference updates only the symlink, not the referenced target.

Which command recursively applies the group of the reference file template.cfg to the directory project/ and all of its contents?

  • A

    chgrp -R --reference=template.cfg project/

  • B

    chgrp --from=template.cfg -R project/

  • C

    chgrp -v project/ template.cfg

  • D

    chgrp --copy-group template.cfg project/

Explanation:

--reference=file copies the group, and -R applies it recursively.

You must update the group ownership of /srv/archive to backupops, but only report files whose group actually changed, ignoring unchanged items. Which command accomplishes this with strict change-logging semantics?

  • A

    chgrp -v backupops /srv/archive

  • B

    chgrp -c backupops /srv/archive

  • C

    chgrp --from=backupops -c /srv/archive

  • D

    chgrp -f -c backupops /srv/archive

Explanation:

-c reports only when a file’s group actually changes, making the output concise and accurate.

Image
Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore