Change a File's Group

Change a File's Group

The report should belong to the shared reports group rather than the teacher's current group. A file has an owning group as well as an owning user. chgrp updates that group ownership when your identity and privileges allow it.

This does not enroll assistants into the group or grant group access automatically. Membership, file group ownership and the group permission triplet are three pieces of a shared-access arrangement. We will change one piece deliberately and verify it.

What you’ll learn

  • Change a file’s owning group with chgrp.
  • Keep file group ownership separate from user group membership.

File group ownership and membership are different

  1. File metadata

    The report has one owning group.

    marks.txt: old group
  2. Change group

    chgrp selects the file’s new owning group when permitted.

    school-reports
  3. Separate membership

    An assistant’s account memberships must still be configured independently.

    no automatic enrollment
A shared group name connects file metadata with account membership, but permissions still decide the allowed operations.

Assign the report to school-reports

Prepare the example

Use the VM's existing school-reports group. An ordinary owner normally needs membership in the selected target group to make this change; otherwise appropriate administrative authority is required. The first line below prepares an owned exercise file, not a report from another person's account.

Bash · Linux terminal

touch marks.txt
chgrp school-reports marks.txt
ls -l marks.txt

Expected behavior; machine-dependent details

The group field reflects school-reports if the change is authorized.

chgrp school-reports marks.txt requests the new group for the file. ls -l then shows the group field beside the owner. If the command is denied, check your ownership, target membership and authority instead of assuming the group name itself is incorrect.

The group mode bits still need to permit the intended operation. A file assigned to the report group with no group read bit will not grant read merely because the group name looks right. Similarly, changing one file's group does not change the assistant's account memberships. Inspect those pieces separately when testing collaboration.

Verify the target group and update file metadata

chgrp changes only the group ownership of a file or directory. It is useful for shared work where several users receive access through a common group.

Step 1: Check the current group ownership

First, let’s create a sample file so we can experiment.

touch sample.txt
ls -l sample.txt
-rw-r--r-- 1 youruser yourgroup 0 Jul  2 10:00 sample.txt

Here, yourgroup is the current group. Let’s say we want to change it to a group called devteam.

Step 2: Check if the group exists

getent group devteam
devteam:x:1002:alice,bob

If you don’t see output like this, you might need to ask your system admin to create the group or use sudo groupadd devteam if you have permissions.

Step 3: Change the group ownership using chgrp

sudo chgrp devteam sample.txt
ls -l sample.txt
-rw-r--r-- 1 youruser devteam 0 Jul  2 10:00 sample.txt

Now you can see the group has changed from yourgroup to devteam. Great job!

Bonus: Change group ownership recursively for a directory

If you want to change group ownership for all files in a folder:

sudo chgrp -R devteam /path/to/folder

The -R flag stands for “recursive” and will apply the group change to all files and subdirectories.

Assign group ownership with chgrp

  • chgrp changes group ownership of files and directories
  • Use ls -l to check current ownership
  • Use sudo chgrp groupname filename to change group
  • Add -R for recursive changes on folders

Try it, then check the worked answer

Your task: Does chgrp automatically add every assistant to school-reports?

Use the VM's existing school-reports group. An ordinary owner normally needs membership in the selected target group to make this change; otherwise appropriate administrative authority is required. The first line below prepares an owned exercise file, not a report from another person's account.

Bash · complete example for this question

touch marks.txt
chgrp school-reports marks.txt
ls -l marks.txt

Expected behavior; machine-dependent details

The group field reflects school-reports if the change is authorized.

Worked answer

No; account membership is separate.

No, chgrp does not add assistants to school-reports. It updates the file's group metadata. Use a membership operation when the account needs to join the role, then verify the actual access rules on the report.

Check your decision

Does chgrp automatically add every assistant to school-reports?
Choose one answer. You can try again.

Answer and explanation

No; account membership is separate.

No, chgrp does not add assistants to school-reports. It updates the file's group metadata. Use a membership operation when the account needs to join the role, then verify the actual access rules on the report.

Changing a file’s group does not enroll accounts

A shared group name connects file metadata with account membership, but permissions still decide the allowed operations. Change the intended file group, read it back, and check the relevant group bits. Do not confuse creating a role, enrolling a person and assigning a resource to that role.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Understand Read, Write and Execute →