Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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); chgrp is 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.

chgrp is a subset of chown: chgrp devs file does exactly what chown :devs file does. 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 -l shows 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 -lR or find first 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 devs freely because devs is in her group list.
  • groups lists every group she belongs to; gid is 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

NeedUse
Change only the groupchgrp or chown :g
Change only the ownerchown owner
Change owner + groupchown user:group
Change group recursivelychgrp -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 use sudo.
  • 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 chgrp when you meant chown. chgrp never changes the owner; if both need fixing, use the user:group form of chown.
  • 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.

PageWhy it’s related
chownChange owner and/or group — the superset of this tool
chmodThe permission bits that group ownership controls
idSee which groups you belong to
sudoNeeded to change groups on files you don’t own
lsls -l shows the current group

chgrp Cheatsheet

CommandMeaning
chgrp devs fileChange the file’s group to devs
chgrp -R devs dir/Recursively change a tree’s group
chgrp :devs fileSame, in chown’s group-only syntax
idList the groups you belong to
ls -l fileVerify the current group
sudo chgrp root fileChange group with elevated privileges