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 Cheat Sheet

TaskCommand
Change group ownership of a filechgrp groupname filename
Change group ownership of multiple fileschgrp groupname file1 file2 file3
Change group ownership of a directorychgrp groupname directory
Change group ownership recursivelychgrp -R groupname directory
Change group recursively with verbose outputchgrp -Rv groupname directory
Change group only if current group matcheschgrp --from=oldgroup newgroup filename
Change group using reference filechgrp --reference=referencefile filename
Change group using numeric GIDchgrp 1001 filename
Change group of symbolic link itselfchgrp -h groupname symlink
Display verbose output during operationchgrp -v groupname filename
Display changes only when modification occurschgrp -c groupname filename
Suppress most error messageschgrp -f groupname filename
Prevent recursive operations on /chgrp -R --preserve-root groupname /path
Allow recursive operations on /chgrp -R --no-preserve-root groupname /path
Change group for all files in current directorychgrp groupname *
Change group ownership recursively for directory treechgrp -R groupname /var/www

chgrp Command Examples Cheat Sheet (Printable)

Common usage patterns:

text
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:

text
chgrp [OPTION] GROUP FILE

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

text
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:

text
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:

text
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

CommandWhat it changesTypical usage
chgrpGroup onlyShared files and directories
chownUser and groupOwnership transfer

Example with chown:

text
sudo chown user:group file.txt

Example with chgrp:

text
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:

text
sudo chgrp developers report.txt

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

You can verify the change using:

text
ls -l report.txt

Multiple files example

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

text
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:

text
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:

text
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

When using recursive operations with -R, the chgrp command can operate on the root directory (/) if it is specified explicitly.

To prevent accidental modification of the entire filesystem, the --preserve-root option can be used.

Example:

text
sudo chgrp -R --preserve-root developers /

With this option enabled, the command will fail if / is specified as the target directory.

By default, GNU chgrp uses --no-preserve-root, meaning the root directory is not treated specially.


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:

text
sudo chgrp --reference=source_file target_file

Practical automation example

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

Example:

text
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:

text
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:

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

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

text
sudo chgrp -c developers file.txt

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

Silent mode (-f)

Suppresses most error messages.

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