Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

chgrp

Change the group of files and directories.

Level
RHCSA exam
Group
Permissions
Needs root
For changes
Package
coreutils

What it does

chgrp GROUP FILE sets the group. root can set any group. A normal user can change the group of an own file, but only to a group he is a member of.

It is the same as chown :GROUP FILE.

How to write it

chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE...

Options 14

Most used

-R, --recursive
Change the whole tree.
-v, --verbose
Report every file.
-c, --changes
Report only changes.
-f, --silent, --quiet
Do not show most errors.
-h, --no-dereference
Change a link itself.
--dereference
Change the target of a link (default).
--reference=RFILE
Use the group of RFILE.
--preserve-root
Refuse to work recursively on /.
--no-preserve-root
Do not treat / special (default).
-H
With -R: follow a link given on the command line.
-L
With -R: follow every link to a directory.
-P
With -R: follow no links (default).
--help
Show help.
--version
Show the version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Give a folder to a team

    chgrp sales /srv/sales

    Combine with chmod 2770 for a shared folder.

  2. 2Change a whole tree

    chgrp -R sales /srv/sales

    All files and folders inside.

  3. 3As a normal user, for your own file

    chgrp sales plan.txt

    Works only if you are in sales.

  4. 4Error when you are not a member

    chgrp wheel plan.txt
    chgrp: changing group of 'plan.txt': Operation not permitted

    Join the group first.

  5. 5Show what changed

    chgrp -Rc sales /srv/sales

    Lists only files that got a new group.

  6. 6Use the group of another file

    chgrp --reference=/srv/sales /srv/sales/import.csv

    Same group as the folder.

  7. 7Fix files after a group GID change

    find /srv -gid 3000 -exec chgrp -h sales {} +

    Files with the old number get the group name again.

  8. 8Group of the web logs for a log team

    chgrp -R logreaders /var/log/httpd; chmod -R g+rX /var/log/httpd

    Read access without root (check that the logs rotate with the same group).

  9. 9Group for a config read by a service

    chgrp nginx /etc/pki/tls/private/site.key; chmod 640 /etc/pki/tls/private/site.key

    The service reads the key; others cannot.

  10. 10Find files of a group (audit)

    find /srv -group sales -ls

    Shows everything the group owns.

Exit codes

0 :: Done. 1 :: Error.