0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use chgrp Command in Linux: Syntax and Examples

Use the chgrp command in Linux to change the group ownership of a file or directory. The basic syntax is chgrp [options] group_name file_name, where you specify the new group and the target file or directory.
📐

Syntax

The basic syntax of the chgrp command is:

  • chgrp [options] group_name file_name

Here:

  • group_name: The name of the new group you want to assign.
  • file_name: The file or directory whose group ownership you want to change.
  • options: Optional flags to modify behavior, like -R for recursive changes.
bash
chgrp [options] group_name file_name
💻

Example

This example changes the group ownership of a file named report.txt to the group staff. It shows how to use chgrp simply and how to check the change.

bash
touch report.txt
ls -l report.txt
sudo chgrp staff report.txt
ls -l report.txt
Output
-rw-r--r-- 1 user user 0 Jun 10 12:00 report.txt -rw-r--r-- 1 user staff 0 Jun 10 12:00 report.txt
⚠️

Common Pitfalls

Common mistakes when using chgrp include:

  • Trying to change group without proper permissions (you need to own the file or be root).
  • Using a group name that does not exist on the system.
  • Forgetting to use -R when changing groups on directories and their contents recursively.

Example of a wrong and right way:

bash
chgrp unknowngroup file.txt
# Error: invalid group

sudo chgrp -R staff /path/to/directory
Output
chgrp: invalid group: 'unknowngroup'
📊

Quick Reference

OptionDescription
-RChange group ownership recursively for directories and their contents
-vVerbose mode, shows files as they are changed
-cLike verbose but reports only when a change is made
--helpDisplay help information about chgrp command

Key Takeaways

Use chgrp group_name file_name to change the group ownership of a file or directory.
You need to own the file or have root permissions to change its group.
Use the -R option to change groups recursively in directories.
Check the group change with ls -l to confirm the new group ownership.
Avoid using non-existent group names to prevent errors.