chgrp command

Change the group ownership of files and directories.

Overview

The chgrp command changes the group ownership of files and directories. It allows users with appropriate permissions to modify which group has access to specific files or directories, which is useful for managing file permissions and access control in multi-user environments.

Options

-c, --changes

Display diagnostic messages only when a change is made.

$ chgrp -c staff document.txt
changed group of 'document.txt' from 'users' to 'staff'

-f, --silent, --quiet

Suppress most error messages.

$ chgrp -f nonexistentgroup file.txt

-v, --verbose

Output a diagnostic message for every file processed.

$ chgrp -v developers scripts/
changed group of 'scripts/' from 'users' to 'developers'

-R, --recursive

Operate on files and directories recursively.

$ chgrp -R developers project/

-h, --no-dereference

Affect symbolic links instead of referenced files.

$ chgrp -h staff symlink.txt

--reference=RFILE

Use RFILE's group instead of specifying a group name.

$ chgrp --reference=template.txt newfile.txt

Usage Examples

Basic Group Change

$ ls -l document.txt
-rw-r--r--  1 user  users  1024 May 5 10:30 document.txt
$ chgrp developers document.txt
$ ls -l document.txt
-rw-r--r--  1 user  developers  1024 May 5 10:30 document.txt

Changing Group Recursively

$ chgrp -R webadmin /var/www/html
$ ls -l /var/www/html
total 16
drwxr-xr-x  3 www-data  webadmin  4096 May 4 14:22 css
drwxr-xr-x  2 www-data  webadmin  4096 May 4 14:22 js
-rw-r--r--  1 www-data  webadmin  8192 May 5 09:15 index.html

Using Numeric Group ID

$ chgrp 1001 config.ini
$ ls -l config.ini
-rw-r--r--  1 user  1001  512 May 5 11:45 config.ini

Tips:

Use Numeric Group IDs for Consistency

When scripting or working across systems, using numeric group IDs (GIDs) instead of names can be more reliable, as group names might differ between systems while GIDs are consistent.

Check Group Membership First

Before changing a file's group, ensure the owner is a member of the target group. Use the groups command to check which groups a user belongs to.

Combine with chmod for Complete Permission Management

Often, you'll want to change both group ownership and permissions. Use chgrp followed by chmod g+rw to give the new group read and write permissions.

Preserve Root Directory Permissions

When using -R on system directories, be careful not to change the group of critical system files, which could affect system stability.

Frequently Asked Questions

Q1. What's the difference between chgrp and chown?

A. chgrp only changes the group ownership of files, while chown can change both the user and group ownership.

Q2. Can any user change the group of a file?

A. No. Only the file owner or root can change a file's group, and the owner can only assign groups they belong to.

Q3. How do I see which groups I can assign to files?

A. Use the groups command to see which groups you belong to.

Q4. Does changing a directory's group affect files inside it?

A. No, unless you use the -R (recursive) option, changing a directory's group doesn't affect the files inside it.

References

https://www.gnu.org/software/coreutils/manual/html_node/chgrp-invocation.html

Revisions