Skip to content

chgrp command in Linux with Examples

chgrp command in Linux with Examples
Share

Reading Time: 2 minutes

In this tutorial “chgrp command in Linux with Examples”, We will show you how to change group ownership of a file or directory.

Introduction:

In the realm of Linux file management, the ‘chgrp’ command emerges as a powerful tool for altering group ownership. Group ownership is a crucial aspect of file permissions, defining which users share access to specific files. This comprehensive guide explores the ‘chgrp’ command, providing insights into its syntax, applications, and practical examples.

Understanding Group Ownership in Linux:

In Linux, every file and directory is associated with a user and a group. While ‘chown’ addresses changes to user ownership, ‘chgrp’ focuses exclusively on altering group ownership. This distinction allows for fine-grained control over file access within a Linux system.

Chgrp Command Syntax

$ chgrp [options] new_group file/directory

The syntax for the ‘chgrp’ command is straightforward:

  • new_group: The group to which ownership is to be assigned.
  • file/directory: The target file or directory.

Basic Usage:

To change the group ownership of a file:

$ chgrp new_group myfile

To change the group ownership of a directory and its contents recursively:

$ chgrp -R new_group mydirectory

Common Options:

  1. Reference Options:
    • --reference=file: Sets the group ownership of the target file or directory to match that of the specified reference file.
  2. Preserve Root Ownership:
    • --preserve-root: Ensures that ‘chgrp’ does not operate recursively on ‘/’.
  3. Verbose Output:
    • -v, --verbose: Provides detailed output, displaying the changes made.
  4. Interactive Mode:
    • -c, --changes: Prompts for confirmation before making changes, displaying only modifications.

How to change group ownership in Linux

Check current owner

$ ls -ld /tmp

Change group owner

$ chgrp testertechie /tmp

Validate Group owner in lInux

$ ls -ld /tmp

chgrp command in Linux with Examples

chgrp command in Linux with Examples

Advanced Techniques:

Combining ‘chgrp’ with ‘find’:

Using ‘find’ with ‘chgrp’ allows for advanced group ownership changes based on specific criteria.

$ find /path/to/directory -type f -exec chgrp new_group {} \;

Changing Group Ownership for Multiple Files:

Updating the group ownership for multiple files can be achieved using wildcard characters.

$ chgrp new_group *.txt

Interactive Mode:

The interactive mode prompts for confirmation before making changes, ensuring user verification.

$ chgrp -c new_group myfile

If you want to change the owner of a file or directory in Linux, Please follow “How to change file owner

You can also follow all our tutorials on Linkedin

Conclusion

We hope this tutorial “chgrp command in Linux with Examples” explains you with each step you need to follow while changing the group owner, If you have further questions, please let us know. The ‘chgrp’ command in Linux offers a precise means of managing group ownership, a critical element in securing file access. By understanding its syntax, common options, and practical applications, users gain a powerful tool for efficient file management within a Linux environment.


Share

Leave a Reply

Your email address will not be published. Required fields are marked *

?>