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.
chgrp developers project/
Try it
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.
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
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 startnewgrp 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 002for 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 -Rcan 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 :groupandchown user:groupare equivalent ways to do the same, andchowncan change both at once.- List a file's group with
ls -l,stat -c %G file(name) or%g(number). List members withgetent group NAME.
Related
chown,chmod,groups,id,umask- Lessons: File Permissions and Ownership, Users, Groups and sudo