Master the Linux ‘chgrp’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 2, 2023

--

This article provides a comprehensive guide for newbies on the Linux chgrp command, including its history, purpose, parameters, and common use cases. The chgrp command is used to change the group ownership of files and directories, which is useful for managing access permissions and ensuring the appropriate user groups have access to specific resources. The tutorial covers the most commonly used parameters and provides examples, as well as some tricky skills and important notes to be aware of when using the chgrp command.

Instructions

This article provides a comprehensive introduction to the Linux chgrp command, including its history, usage, parameters, and common use cases.

History

The chgrp command is a part of the GNU Core Utilities package and has been a fundamental command in Unix-based systems since the early days of Unix.

When and why to use it

The chgrp command is used to change the group ownership of files and directories. This is useful for managing access permissions and ensuring that the correct group of users can access specific resources.

How to use it

To use the chgrp command, you need to provide the target group and the files or directories you want to change the group ownership for.

chgrp group_name target_file

The commonly used parameters

  • -R Recursively change the group ownership of directories and their contents.
chgrp -R group_name target_directory
  • -c Display verbose output, showing only files with group ownership changes.
chgrp -c group_name target_file
  • -f Suppress error messages for non-existent or inaccessible files.
chgrp -f group_name target_file
  • -v Display verbose output, showing all processed files.
chgrp -v group_name target_file

Other supported parameters

  • -h Change the group of symbolic links instead of their targets.
  • --reference=REF_FILE Use the group of REF_FILE instead of specifying a group.

Most common use cases

  • Changing the group ownership of a single file.
chgrp new_group file.txt
  • Changing the group ownership of a directory and its contents recursively.
chgrp -R new_group directory_name

The tricky skills

  • Using chgrp with the find command to change the group ownership of specific files based on certain criteria.
find /path/to/dir -type f -name "*.txt" -exec chgrp new_group {} \\;

What needs to be noted

  • Only the superuser or the file’s owner can change the group ownership of a file or directory.
  • The -R flag can lead to unintentional changes if not used carefully. Always double-check your command before using the -R flag.

Conclusion

The chgrp command is a powerful tool for managing group ownership of files and directories in Linux systems. By understanding its parameters and use cases, you can effectively manage access permissions and ensure that the correct group of users can access the necessary resources.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet