Chapter 49: Bash Group (chgrp)
Bash Group (chgrp)
This is a small but very useful command that many beginners skip or forget about, but once you understand it, you’ll see why Linux groups are so powerful — especially when you work in teams, on servers, or share files with other users in Hyderabad or anywhere.
chgrp = change group
It is the command that changes only the group owner of a file or folder. (We already learned chown which can change both user owner and group owner — but sometimes you only want to change the group, and that’s where chgrp shines.)
Quick Reminder – Why Groups Exist in Linux
Every file/folder has two owners:
- User owner (one person — usually the creator)
- Group owner (a group — can have many users as members)
Permissions are split into three parts:
- u = user/owner permissions (first rwx)
- g = group permissions (second rwx)
- o = others permissions (third rwx)
So if 10 people are in the same group (example: “developers”), and a file has group = “developers” and g=rw-, then all 10 people can read and write to that file — even if they are not the user owner.
This is super useful for:
- Team projects (everyone in “developers” can edit code)
- Shared folders on servers (/var/www/ owned by “www-data” group)
- School/college group assignments
- Company file servers
chgrp Syntax (Very Simple)
|
0 1 2 3 4 5 6 |
chgrp [options] group_name file_or_folder |
- You can only change the group to a group you are a member of (unless you are root/sudo)
- Normal users cannot use chgrp to set group to something they don’t belong to
Step-by-Step Examples – Real Hyderabad Developer Style
1. First – Check Your Groups
|
0 1 2 3 4 5 6 7 8 9 |
groups # or more detailed: id -Gn # or just id |
Example output for you:
|
0 1 2 3 4 5 6 |
webliance adm cdrom sudo dip plugdev lpadmin developers sambashare |
→ “webliance” is your primary group, but you are also in “developers”, “sudo”, etc.
2. Change Group of a Single File
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
touch team_note.txt ls -l team_note.txt # → -rw-rw-r-- 1 webliance webliance ... team_note.txt chgrp developers team_note.txt ls -l team_note.txt # → -rw-rw-r-- 1 webliance developers ... team_note.txt |
→ Now the group is “developers” → any member of developers group gets the middle set of permissions (rw- in this case)
3. Change Group of a Whole Folder + Contents (Recursive – Very Important!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
mkdir ~/team_project touch ~/team_project/code.py ~/team_project/readme.md chgrp -R developers ~/team_project/ ls -lR ~/team_project/ # everything inside now has group = developers |
Now add proper permissions so team can actually work:
|
0 1 2 3 4 5 6 7 |
chmod -R 775 ~/team_project/ # → rwxrwxr-x everyone in group can read/write/execute |
4. Real Server Example – Web Hosting
Many web servers use group “www-data” or “nginx” so multiple users can upload files:
|
0 1 2 3 4 5 6 7 |
sudo chgrp -R www-data /var/www/uploads/ sudo chmod -R 775 /var/www/uploads/ |
→ Now anyone in www-data group (like your deploy user) can write to uploads folder, and web server can read/serve them.
5. When chgrp Fails (Common Mistakes)
|
0 1 2 3 4 5 6 7 |
chgrp aliens secret.txt # → chgrp: changing group of 'secret.txt': Operation not permitted |
Why? Because you are not a member of group “aliens”.
Fix: either join the group (needs admin) or use sudo (if allowed):
|
0 1 2 3 4 5 6 |
sudo chgrp aliens secret.txt |
chgrp vs chown (Quick Comparison Table)
| Feature | chgrp | chown |
|---|---|---|
| Changes what? | Only group owner | User owner + group owner (or just one) |
| Needs sudo for user change? | No (only group) | Yes (for changing user) |
| Can normal user use it? | Yes — to groups they belong to | Only for group (same as chgrp) |
| Most common today? | Less used (chown can do it) | More used (chown :group does same job) |
| Recursive flag | -R | -R |
Teacher note: In modern Linux (2020s), most people just use chown :group instead of chgrp — it does exactly the same thing and is more flexible.
So:
|
0 1 2 3 4 5 6 7 |
chown :developers folder/ # same as chgrp developers folder/ |
Both are correct — but chown wins because it can do user + group in one command.
Quick Practice Right Now (Safe & Fun)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 1. Create playground mkdir group_test cd group_test touch file1.txt file2.txt mkdir shared # 2. Check current group ls -l # 3. Change group (pick one you are in — see your `groups` command) chgrp sudo file1.txt ls -l file1.txt # group now sudo # 4. Recursive example chgrp -R developers shared/ ls -ld shared/ # 5. Try invalid group (will fail unless you are in it) chgrp nonexistent file2.txt |
Summary Table – chgrp at a Glance
| Goal | Command Example | Needs sudo? | Common alternative |
|---|---|---|---|
| Change group of one file | chgrp developers myfile.txt | Usually no | chown :developers |
| Change group of folder + contents | chgrp -R developers team_folder/ | No | chown -R :developers |
| Web server upload folder | sudo chgrp -R www-data /var/www/uploads/ | Yes | sudo chown -R :www-data |
| Check which groups you belong to | groups or id -Gn | No | — |
| See current group owner | ls -l or stat file.txt | No | — |
Got it, boss? chgrp (or better — chown :group) is how you share access smartly with a group of people without giving away full user ownership.
Any part confusing? Want next: “Teacher, explain how to create new groups (groupadd)” or “how to add/remove users from groups (usermod / gpasswd)” or “umask – why new files get 664/644 by default” or more ownership practice?
Just say — teacher is ready in Hyderabad! Keep watching that group column in ls -l — it’s the key to team collaboration! 🐧👥🔐 😄
