Linux chgrp Command: 10 Examples with Details

The chgrp command in Linux is used to change the group ownership of files or directories. It is essential for managing permissions in multi-user environments. Below are 10 practical examples demonstrating various uses of the chgrp command.

Example 1: Change Group Ownership of a Single File

chgrp developers document.txt

Description: This command changes the group ownership of document.txt to the developers group. Only the group is changed, not the user ownership.

Verification: Run ls -l document.txt to see the updated group.

Example Output: -rw-r--r-- 1 user developers 0 Oct 09 13:56 document.txt

Example 2: Change Group Ownership of a Directory

chgrp admins /var/www

Description: This command changes the group ownership of the /var/www directory to the admins group. This is useful for shared directories managed by a specific group.

Verification: Run ls -ld /var/www.

Example Output: drwxr-xr-x 2 user admins 4096 Oct 09 13:56 /var/www

Example 3: Recursively Change Group Ownership

chgrp -R developers /home/project

Description: The -R option recursively changes the group ownership of the /home/project directory and all its contents (files and subdirectories) to the developers group.

Verification: Run ls -lR /home/project to confirm the group change for all contents.

Example 4: Change Group Ownership Using Group ID (GID)

chgrp 1001 file.txt

Description: This command changes the group ownership of file.txt to the group with GID 1001. You can find GIDs in /etc/group or by using getent group groupname.

Verification: Run ls -l file.txt.

Example Output: -rw-r--r-- 1 user 1001 0 Oct 09 13:56 file.txt

Example 5: Change Group Ownership for Multiple Files

chgrp editors *.txt

Description: This command changes the group ownership of all .txt files in the current directory to the editors group using a wildcard.

Verification: Run ls -l *.txt to see the group change for all matching files.

Example 6: Preserve Permissions While Changing Group

chgrp --preserve-root editors /data

Description: The --preserve-root option prevents chgrp from modifying the root directory (/) accidentally. This command changes the group of /data to editors.

Verification: Run ls -ld /data.

Example Output: drwxr-xr-x 2 user editors 4096 Oct 09 13:56 /data

Example 7: Change Group Ownership with Reference File

chgrp --reference=template.txt newfile.txt

Description: The --reference option copies the group ownership from template.txt to newfile.txt. This is useful for matching group settings without knowing the group name.

Verification: Run ls -l newfile.txt and compare with template.txt.

Example 8: Suppress Error Messages

chgrp -f developers /nonexistent

Description: The -f (force) option suppresses error messages if the operation fails (e.g., if /nonexistent does not exist). This is useful in scripts to avoid cluttering output.

Output: No output if the file does not exist.

Example 9: Change Group Ownership Verbose Output

chgrp -v admins report.pdf

Description: The -v (verbose) option displays a message for each file processed, confirming the group change for report.pdf to the admins group.

Output: changed group of 'report.pdf' to admins

Example 10: Change Group Ownership for Symbolic Links

chgrp -h editors symlink.txt

Description: The -h option changes the group ownership of the symbolic link symlink.txt itself, rather than the file it points to. This is useful for managing link permissions separately.

Verification: Run ls -l symlink.txt to confirm the group change on the link.

Example Output: lrwxrwxrwx 1 user editors 12 Oct 09 13:56 symlink.txt -> document.txt