This post was originally published on go2linux.org on October 10, 2007. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

Introduction

chgrp changes the group associated with a file or directory. Every file on Linux belongs to both a user (the owner) and a group. chgrp lets you change the group without touching the owner.

Only the file's owner or root can change the group of a file, and a regular user can only change it to a group they belong to.

Syntax

chgrp [options] group file...
chgrp [options] --reference=rfile file...
  • group — the name or numeric GID of the new group
  • file — one or more files or directories to change
  • --reference=rfile — use the group of rfile as the new group

Basic examples

Change the group of a file:

chgrp developers project.py

Change the group of multiple files:

chgrp www-data index.html style.css app.js

Use a numeric GID instead of a group name:

chgrp 1001 /var/app/data

Recursive change

Apply the group change to a directory and all its contents:

chgrp -R www-data /var/www/html/

The -R flag descends into subdirectories recursively.

Copy group from another file

Use --reference to set the same group as another file:

chgrp --reference=/etc/passwd /tmp/myfile

Useful options

Option Effect
-R Recursive — apply to all files in subdirectories
-v Verbose — print each file processed
-c Report only files whose group actually changes
-f Silent — suppress error messages
-h Act on symbolic links themselves, not what they point to
--reference=file Use the group of another file as the target group

Symbolic links

By default, chgrp follows symbolic links and changes the group of the file the link points to. To change the group of the symlink itself:

chgrp -h www-data /var/www/symlink

Check the current group

Use ls -l to see the group assigned to a file:

ls -l project.py
-rw-r--r-- 1 ggarron developers 4096 Jun 14 10:00 project.py

The third column is the owner; the fourth is the group.

Or use stat for more detail:

stat project.py

chgrp vs chown vs chmod

Command Changes
chgrp Group only
chown Owner (and optionally group)
chmod Permissions (read/write/execute)

chown can also change the group with the owner:group syntax:

chown :developers project.py         # same as: chgrp developers project.py
chown ggarron:developers project.py  # changes both owner and group

Use chgrp when you only need to change the group and want to be explicit about it.

List available groups

To see which groups exist on the system:

getent group

To see which groups your user belongs to:

groups

Or for a specific user:

groups username