chgrp Command in Linux Explained: Change Group Ownership with Examples

chgrp Command in Linux Explained: Change Group Ownership with Examples

The chgrp command in Linux is used to change the group ownership of a file or directory. Unlike chown, which can change both user and group, chgrp focuses exclusively on group ownership, making it the preferred tool for managing shared folder permissions without affecting individual file owners.

In Linux, every file has:

  • an owner (user)
  • a group
  • permission bits for user, group, and others

The chgrp command modifies only the group, without affecting:

  • the file owner
  • existing permission bits

This makes chgrp ideal for:

  • shared project directories
  • multi-user environments
  • fixing group ownership without touching user ownership

chgrp Command Quick Reference Table

Option Description
-R Change group ownership recursively
-v Show verbose output
-c Show output only when changes occur
-f Suppress most error messages
-h Change symbolic link ownership
–reference Copy group from another file
–preserve-root Prevent recursive operation on /

chgrp Command Examples Cheat Sheet (Printable)

Common usage patterns:

chgrp group file
chgrp group file1 file2
chgrp group directory
chgrp -R group directory
chgrp --reference=source target
chgrp -h group symlink

chgrp Command Syntax and Usage

Basic syntax

The general syntax of the chgrp command is:

chgrp [OPTION] GROUP FILE

When operating on system files or files owned by other users, sudo is typically required:

sudo chgrp GROUP FILE

Using group name vs GID

You can specify the target group in two ways.

Using group name (recommended)

This is the most readable and commonly used method:

sudo chgrp developers report.txt

Here:

  • developers is the group name
  • report.txt is the target file

Using group ID (GID)

You can also use the numeric group ID:

sudo chgrp 1001 report.txt

Using GID is useful in scripts or automation, but group names are preferred for clarity and maintainability.


chgrp vs chown: Key Differences Explained

The chgrp and chown commands serve different purposes.

chgrp vs chown comparison

Command What it changes Typical usage
chgrp Group only Shared files and directories
chown User and group Ownership transfer

Example with chown:

sudo chown user:group file.txt

Example with chgrp:

sudo chgrp group file.txt

How to Change Group Ownership Using chgrp

Change Group Ownership of a File

You can use the chgrp command to change the group ownership of one or more files.

Single file example

To change the group ownership of a single file:

sudo chgrp developers report.txt

After execution, the file report.txt will belong to the developers group.

You can verify the change using:

ls -l report.txt

Multiple files example

You can assign the same group to multiple files at once by listing them as arguments:

sudo chgrp developers file1.txt file2.txt file3.txt

All listed files will now share the same group ownership.

Change Group Ownership of a Directory

To change the group ownership of a directory:

sudo chgrp developers project_dir

This command changes the group ownership of the directory itself, but does not affect files inside the directory.


Use chgrp Recursively (With Safety Tips)

The recursive option allows chgrp to change the group ownership of a directory and all its contents, including subdirectories and files.

Recursive directory change

To change group ownership recursively:

sudo chgrp -R developers project_dir

This command:

  • changes the group of project_dir
  • updates all files and subdirectories inside it

Recursive usage is common for:

  • project folders
  • shared team directories
  • application deployment paths

–preserve-root explained

By default, recursive operations also apply to the root directory (/) if explicitly specified. This can be dangerous.

The --preserve-root option prevents recursive operations on /:

sudo chgrp -R --preserve-root developers /

If / is passed accidentally, the command will fail instead of changing group ownership across the entire system.

Using --preserve-root is a recommended safety practice when working with recursive commands.


Copy Group Ownership From Another File (--reference)

Instead of specifying a group name, you can copy the group ownership from an existing file or directory.

Syntax:

sudo chgrp --reference=source_file target_file

Practical automation example

This approach is useful in scripts where consistent group ownership is required.

Example:

sudo chgrp --reference=template.conf app.conf

Here:

  • template.conf provides the group
  • app.conf receives the same group ownership

This avoids hardcoding group names and improves portability.


Symbolic links behave differently from regular files when using chgrp.

Default behavior

By default, chgrp changes the group ownership of the target file that the symbolic link points to, not the link itself.

Example:

sudo chgrp developers symlink_file

The group ownership of the original file is changed.

–no-dereference (-h) explained

To change the group ownership of the symbolic link itself, use the -h or --no-dereference option:

sudo chgrp -h developers symlink_file

This ensures:

  • only the symbolic link is modified
  • the target file remains unchanged

This is useful when managing symbolic links in configuration directories or deployment setups.


Control chgrp Output (Verbose, Changes, Silent)

The chgrp command provides multiple options to control how much information is displayed during execution.

Verbose mode (-v)

Displays information for every file processed, whether or not a change was made.

sudo chgrp -v developers file.txt

Use this when you want full visibility of what chgrp is doing.

Changes only (-c)

Displays output only when a group change actually occurs.

sudo chgrp -c developers file.txt

This is useful in scripts or logs to avoid unnecessary output.

Silent mode (-f)

Suppresses most error messages.

sudo chgrp -f developers file.txt

Note:

  • Syntax errors or invalid group errors may still appear
  • Useful when processing many files and ignoring non-critical failures

Frequently Asked Questions

1. What does the chgrp command do in Linux?

The chgrp command is used to change the group ownership of files and directories in Linux without modifying the file owner.

2. What is the difference between chgrp and chown?

chgrp changes only the group ownership, whereas chown can change both user and group ownership of a file or directory.

3. Does chgrp require sudo?

Yes, sudo is required when changing the group ownership of files not owned by the current user or when assigning a group the user is not a member of.

4. How do I change group ownership recursively?

Use the -R or –recursive option with chgrp to change the group ownership of a directory and all its files and subdirectories.

5. Does chgrp follow symbolic links?

By default, chgrp changes the group of the target file. Use -h or –no-dereference to change the group ownership of the symbolic link itself.

Conclusion

The chgrp command is a safe and efficient way to manage group ownership in Linux systems.

It is best suited for:

  • shared directories
  • team-based workflows
  • permission management without changing file owners

By understanding recursive behavior, symbolic link handling, and proper use of sudo, you can use chgrp confidently across Linux distributions.

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.