Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Permissions and Users

Linux chgrp command

Reference for the Linux chgrp command: change the group of files and directories, recurse, use a reference file, and combine it with setgid for shared folders.

chgrp changes the group that owns a file, leaving the owning user alone. It is the same as chown :group file, and exists because group-only changes are common: sharing a project directory with a team means giving the directory to the team's group and letting the group's permission bits do the work.

Rules for who may do it: root can set any group; the file's owner can set the group to any group they are a member of. Anyone else, or an owner picking a group they do not belong to, gets Operation not permitted.

Terminal
chgrp developers project/

Try it

Bash Changing and checking the group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Recursion and the setgid bit on a shared directory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE...
Name Description
GROUP Group name or numeric group ID.
-R, --recursive Recurse into directories.
-v, --verbose Report each file processed.
-c, --changes Report only files whose group changed.
-f, --silent Suppress most error messages.
-h, --no-dereference Affect symlinks themselves rather than their targets.
-H, -L, -P With -R: follow command-line symlinks, follow all, or follow none (default).
--from=CURRENT Only change files currently in this group.
--reference=RFILE Use the group of RFILE.
--preserve-root Refuse to operate recursively on /.

Returns: 0 on success, non-zero if any file could not be changed.

Errors: invalid group for an unknown name, Operation not permitted when you are not allowed to use that group, No such file or directory.

Usage notes

  • Group membership is read at login. After usermod -aG team you, your running shells do not see the new group. Log in again or start newgrp team/sg team -c 'cmd'.
  • A team directory needs three things: the group set (chgrp), group write permission (chmod g+w) and the setgid bit on directories (chmod g+s) so new files inherit the group. Without setgid new files get the creator's primary group.
  • Also set umask 002 for people working in the shared area, otherwise newly created files are not group-writable.
  • Changing the group clears setuid and setgid on regular files when done by a non-root user; directories keep their setgid bit.
  • chgrp -R can be slow and dangerous on large trees. Check the target twice and use --from= to limit the change.
  • Numeric IDs always work, which is useful in containers or when a group name is not in /etc/group.
  • chown :group and chown user:group are equivalent ways to do the same, and chown can change both at once.
  • List a file's group with ls -l, stat -c %G file (name) or %g (number). List members with getent group NAME.