chgrp
Change the group ownership of a file or directory
By CMD Script Team · 3 min read · Last updated
chgrp [OPTIONS] GROUP FILE...Options
| Flag | Description |
|---|---|
-R | Change group ownership recursively through a directory tree |
-v | Verbose: explain what is being done for each file |
-c | Like verbose, but report only when a change is actually made |
--reference=FILE | Use the same group as the given reference file instead of naming one |
-h | Change the group of a symbolic link itself, not the file it points to |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
chgrp ("change group") changes which group owns a file or directory, without touching
the user owner or the permission bits. Group ownership matters because Unix permissions
grant a separate set of read/write/execute bits to "the group" — changing a file's group
changes which set of users that middle permission block applies to.
Beginner examples
chgrp developers file.txt— change a file's group todeveloperschgrp staff *.log— change the group of every matching log filechgrp -R developers /srv/app— recursively change group for a directory treels -l file.txt— check the current owner and group before/after changing
chgrp developers shared_report.xlsx
Advanced examples
- Recursively update a whole project directory's group after a team reorganization:
chgrp -R webteam /var/www/myapp - Copy the group from an existing, correctly-configured file:
chgrp --reference=good_file.txt new_file.txt - Change the group of a symlink itself rather than its target:
chgrp -h groupname symlink - Combine with
findfor selective recursive changes:find /srv/app -type f -name "*.log" -exec chgrp appgroup {} \; - Verify the effect with verbose mode:
chgrp -v developers *.conf
find /srv/app -type d -exec chgrp -R webteam {} \;
Common mistakes
- Confusing
chgrpwithchownand expecting it to also change the file's owner — it only touches the group field. - Trying to set a group you're not a member of without sufficient privileges and getting a permission-denied error instead of realizing root/sudo is required.
- Forgetting
-Rwhen trying to update an entire directory's contents, leaving nested files with the old group. - Assuming a group change alone grants group members access — the group's permission
bits (via
chmod) still need to allow the desired access.
Tips
- Use
chown user:group filewhen you need to change both the owner and group at once — it's a single command instead of separatechownandchgrpcalls. - Pair
chgrp -Rwithchmod -R g+rwXwhen onboarding a new team to a shared directory, so the group not only owns the files but can actually read/write them. ls -lshows the group in the second ownership column — always verify with it after runningchgrp.
Best practices
- Prefer
chown user:groupover separatechown/chgrpcalls when both need to change, to keep the operation atomic and the intent clear. - When managing shared team directories, standardize on a dedicated group (e.g.
webteam) and applychgrp -Rplus appropriatechmodgroup bits rather than granting broad "other" permissions. - Audit group ownership periodically on sensitive directories with
find dir -not -group expectedgroupto catch drift.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Handing off a project directory to a different team by recursively changing its group:
chgrp -R newteam /srv/project. - Fixing deployment scripts that create files owned by the wrong group, breaking a shared web server's read access.
- Setting up a shared directory where multiple users in the same group need collaborative read/write access to uploaded files.
Common interview questions
- What's the difference between chgrp and chown?
chgrpchanges only the group owner;chowncan change the user owner, group owner, or both (chown user:group file). - What permissions do you need to change a file's group? You generally need to own the file and be a member of the target group, or have root/sudo privileges to assign any group.
- How would you recursively change the group of a whole directory?
chgrp -R groupname directory/.
Frequently Asked Questions
What's the difference between chgrp and chown?
chgrp only changes the group owner of a file. chown can change the user owner, the group owner, or both at once (e.g. chown user:group file), making it a superset of what chgrp does.
Do I need to be root to use chgrp?
You can change a file's group to any group you belong to if you own the file, but changing to a group you're not a member of typically requires root/sudo privileges.
How do I change the group of an entire directory tree?
Use chgrp -R groupname directory/ to recursively apply the change to the directory and everything inside it.
Can I copy the group from another file instead of typing its name?
Yes, use chgrp --reference=other_file target_file to set target_file's group to match other_file's group.
Cheat sheet
Download a quick-reference cheat sheet for chgrp.