chgrp Command Guide for Changing a File’s Group
chgrp changes the group that owns a file or directory — nothing else.
What is chgrp, and Why Use It?
Every file has an owner and a group. The group is a named collection of users, and its members get whatever group permissions the file grants. chgrp (“change group”) moves a file from one group to another, without touching the owner or the permission bits.
It’s the narrow tool for a narrow job: you want a colleague’s team (or a service account) to have access to a file, and you don’t want to change who owns it.
Why Learn chgrp?
- Sharing files between users — put a file in a shared group and give that group
rw-permissions. - Team directories — projects usually have a group (
devs,wheel,staff);chgrpis how files join it. - Service accounts — web files often need group ownership by the web server’s group.
- It pairs with
chown, which can do the same thing plus more.
chgrpis a subset ofchown:chgrp devs filedoes exactly whatchown :devs filedoes. Learn both; use whichever is clearer in context.
1. Change a File’s Group
chgrp newgroup file
Example:
# The file currently belongs to group alice; move it to devs
chgrp devs notes.txt
# Verify
ls -l notes.txt
- The output of
ls -lshows the group in the fourth column:
$ ls -l notes.txt
-rw-r--r-- 1 alice devs 412 Dec 4 09:12 notes.txt
- To change a group on a file you don’t own, you need to be a member of the target group or use
sudo.
2. Change a Group Recursively
Directories propagate to everything inside them with -R:
chgrp -R devs project/
Example:
# Hand a whole shared directory to the devs group
sudo chgrp -R devs /srv/shared/
- ⚠️ As with
chown -R, always double-check the path — a recursive change can’t be undone without redoing it. - Preview with
ls -lRorfindfirst if the tree is large.
3. Check Group Membership First
chgrp only lets you move a file to a group you belong to (unless you’re root). Check your groups with id:
id
Example:
$ id
uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),999(devs)
- Alice can
chgrp devsfreely becausedevsis in her group list. groupslists every group she belongs to;gidis her primary group.
4. List a Directory’s Groups
ls -l shows the group per file; ls -nd shows the numeric IDs, which is useful when names are ambiguous:
ls -l dir/
ls -nd dir/
Example:
$ ls -nd project/
drwxrwxr-x 2 1000 1000 4096 Dec 4 09:12 project/
- The numeric form is handy in scripts and when debugging group IDs that don’t resolve to names.
5. chgrp vs chown
| Need | Use |
|---|---|
| Change only the group | chgrp or chown :g |
| Change only the owner | chown owner |
| Change owner + group | chown user:group |
| Change group recursively | chgrp -R / chown -R :g |
Example:
chgrp devs file # group only
chown alice file # owner only
chown alice:devs file # both at once
Common Pitfalls
- Trying to change to a group you’re not in. You’ll get “Operation not permitted” — add yourself to the group with
usermod(as root) or usesudo. - Forgetting that group membership affects permissions. Moving a file to a group doesn’t grant access unless the group permission bits allow it — check with
ls -l. - Using
chgrpwhen you meantchown.chgrpnever changes the owner; if both need fixing, use theuser:groupform ofchown. - Recursive changes on system paths. Same warning as
chown -R: verify the path character by character. - Assuming the change is instant for running processes. Already-open handles keep the old group; new accesses see the change.
Related Pages
| Page | Why it’s related |
|---|---|
| chown | Change owner and/or group — the superset of this tool |
| chmod | The permission bits that group ownership controls |
| id | See which groups you belong to |
| sudo | Needed to change groups on files you don’t own |
| ls | ls -l shows the current group |
chgrp Cheatsheet
| Command | Meaning |
|---|---|
chgrp devs file | Change the file’s group to devs |
chgrp -R devs dir/ | Recursively change a tree’s group |
chgrp :devs file | Same, in chown’s group-only syntax |
id | List the groups you belong to |
ls -l file | Verify the current group |
sudo chgrp root file | Change group with elevated privileges |