Linux Command #48 – chgrp (Linux OS)

Editorial photograph of a Linux terminal displayed on a computer screen.

chgrp changes the group ownership of files and directories. It is a natural follow-up to Linux Command #47 — newgrp. newgrp changes the active group context of a shell session; chgrp changes the group recorded on a filesystem object.

Computer screen displaying terminal prompts in a technical workspace.
Terminal-based ownership and permissions work is a routine part of Linux administration. Photo: Bernd Dittrich / Unsplash.

Basic Syntax

chgrp GROUP FILE

The GNU Coreutils manual defines chgrp as changing the group ownership of each specified file. The group can normally be written as a group name or numeric group ID. The current Linux manual documents the general form as chgrp [OPTION]... GROUP FILE....

chgrp developers report.txt

This changes only the group ownership of report.txt to developers. It does not change the file’s user owner and it does not automatically change the file’s permission bits.

A focused demonstration of chgrp, including single-file changes, verification with ls -l, and recursive directory use.

Group Ownership Is Not the Same as Permission

A Linux file has an owning user, an owning group, and permission bits. chgrp changes the owning group. Whether members of that group can read, write, or execute the file still depends on the group permission bits. That is why chgrp is often used alongside chmod, but the two commands perform different jobs.

ls -l report.txt
chgrp developers report.txt
ls -l report.txt

Always verify the result instead of assuming the change worked. ls -l is the quick check; stat report.txt gives a more detailed view of ownership and metadata.

Changing a Directory Group

chgrp developers /srv/project

This changes the group on the directory itself. It does not automatically walk through every file already inside the directory. For an existing tree, GNU chgrp provides the recursive -R option.

chgrp -R developers /srv/project

Recursive ownership changes deserve care. Before using -R, confirm the starting path with commands such as pwd, ls -ld, or find. A typo in a privileged recursive command can affect a much larger part of the filesystem than intended.

Use –reference to Copy Group Ownership

GNU chgrp can copy the group ownership from another file instead of requiring you to type the group name manually. This is useful when matching an existing file or directory that already has the desired group.

chgrp --reference=known-good.txt new-file.txt

Afterward, verify both objects with ls -l or stat. The --reference form reduces guessing when you want one object to follow another object’s group ownership.

Verbose and Changes-Only Output

  • -v or --verbose — report every file processed.
  • -c or --changes — report only when a change is actually made.
  • -f or --silent/--quiet — suppress most error messages.
chgrp -v developers report.txt
chgrp -c developers *.log

For administration work, -v and -c can make a change easier to audit. They do not replace verification, but they give immediate feedback about what the command processed.

Eli the Computer Guy — Linux ownership, groups, chown, and chmod, providing the broader ownership-and-permissions context for chgrp.

chgrp vs. chown

chgrp is dedicated to changing the group owner. GNU chown can change the user owner, the group owner, or both. For example, chown :developers report.txt changes only the group and therefore overlaps with what chgrp developers report.txt does. Using chgrp can make your intent clearer when group ownership is the only attribute you want to change.

Symlinks and Recursive Traversal

Recursive ownership commands interact with symbolic links, so GNU chgrp provides traversal controls such as -H, -L, and -P. The current GNU manual documents -P as the default recursive behavior. Treat -L with special caution because following every symbolic link encountered can cause a recursive operation to reach locations outside the directory tree you thought you were changing.

A directly relevant LinuxQuestions discussion shows the practical relationship between changing a file’s group ownership with chgrp and then assigning the appropriate group permission bits.

A Shared-Directory Example

sudo chgrp developers /srv/project
sudo chmod 2770 /srv/project
ls -ld /srv/project

Here, chgrp assigns the directory to the developers group. The chmod 2770 example is a separate permission change: it gives owner and group full directory access, blocks access for others, and sets the set-group-ID bit on the directory so newly created entries can inherit the directory’s group. Use values appropriate to the actual system rather than copying permission modes blindly.

Quick Lab

  1. Create a harmless test file: touch chgrp-test.txt.
  2. Inspect it with ls -l chgrp-test.txt.
  3. Choose a group your account is allowed to assign.
  4. Run chgrp groupname chgrp-test.txt.
  5. Run ls -l chgrp-test.txt again and identify the changed group field.
  6. Run stat chgrp-test.txt for a more detailed ownership check.
  7. Delete the test file when finished.

Knowledge Check + Answers

  1. What does chgrp change? The group ownership of a file or directory.
  2. Does chgrp automatically give the group read or write access? No. Permission bits are controlled separately.
  3. Which option changes a directory tree recursively? -R or --recursive.
  4. How can you copy group ownership from another file? Use --reference=RFILE.
  5. Which command can also change group ownership while optionally changing user ownership? chown.
  6. What should you do after changing ownership? Verify the result with tools such as ls -l or stat.

Prior Linux Command Lessons

Primary References

Editor’s Note

The featured image and body image are separate. The lesson uses normal responsive Gutenberg headings, paragraphs, lists, images, embeds, and code blocks for actual commands. No ordinary lesson prose is placed inside decorative text boxes.

BitcoinVersus.Tech is not a financial advisor. Content is provided for informational and educational purposes.

Leave a comment