Skip to content

chgrp Linux Command Guide

The chgrp command in Linux is used to change the group ownership of files and directories. It allows users to assign a new group to a specified file or directory. By utilizing the chgrp command, you can easily modify the group ownership of multiple files at once, streamlining group management tasks. This command is especially useful in a shared working environment where users may need to collaborate on files within the same group.

chgrp Syntax:

Terminal window
chgrp [options] group file

Options:

OptionDescription
-cReport only when a change is made
-fSilence most error messages
-hChange the group of symbolic links only (default behavior)
-RRecursively change group of directories and their contents
—reference=reference_fileSet group to that of reference_file
—dereferenceAffect symlinks instead of references
—no-dereferenceAffect references instead of symlinks
—preserve-rootFail to operate recursively on ’/‘

Parameters:

ParameterDescription
groupThe group to which to change ownership to
fileThe file or directory to change the group ownership of

chgrp Command Samples:

Change Group Ownership of a File

Terminal window
chgrp staff file.txt

Changes the group ownership of the file “file.txt” to the group “staff”.

Change Group Ownership Recursively

Terminal window
chgrp -R admin documents/

Changes the group ownership of all files and subdirectories in the “documents” directory to the group “admin” recursively.

Display Verbose Output

Terminal window
chgrp -v users file.txt

Displays verbose output when changing the group ownership of the file “file.txt” to the group “users”.

Preserve File Metadata

Terminal window
chgrp --preserve-root wheel /

Changes the group ownership of the root directory (”/”) to the group “wheel” while preserving the root directory permissions.

Terminal window
chgrp games game_link

Changes the group ownership of a symbolic link named “game_link” to the group “games”.

Change Group Ownership Using Numeric Group ID

Terminal window
chgrp 1001 data.csv

Changes the group ownership of the file “data.csv” to the group ID “1001”.

Change Group Ownership using Symbolic Reference

Terminal window
chgrp :finance file.txt

Changes the group ownership of the file “file.txt” to the group “finance” using the symbolic reference.

chgrp FAQ:

How do I use chgrp in Linux?

To use the chgrp command in Linux, execute the following command:

Terminal window
chgrp newgroup file.txt

How do I change the group ownership recursively with chgrp?

To change the group ownership recursively for all files and directories within a specified directory, use the recursive flag (-R) with chgrp:

Terminal window
chgrp -R newgroup directory

How can I specify a specific group by GID with chgrp?

To change the group ownership of a file to a specific group identified by its Group ID (GID), use the numeric group ID with the chgrp command:

Terminal window
chgrp :1000 file.txt

How do I preserve the original file’s group with chgrp?

To avoid changing the group ownership of a file while modifying its permissions, use the —reference option with chgrp to copy the group from a reference file:

Terminal window
chgrp --reference=referencefile file.txt

How can I display verbose output while using chgrp in Linux?

To display detailed information about the group changes being made by the chgrp command, use the verbose flag (-v) along with the command:

Terminal window
chgrp -v newgroup file.txt

To change the group ownership of a symbolic link itself without following the link to the target file, use the —no-dereference option with chgrp:

Terminal window
chgrp --no-dereference newgroup symlink

Applications of the chgrp command

  • Change the group ownership of a file or directory
  • Change the group ownership of multiple files or directories
  • Recursively change the group ownership of a directory and its contents