Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux chgrp Command

Linux chgrp Command

Linux Command Reference Linux Command Reference


The Linux chgrp (short for "change group") command is used to change the group ownership of a file or directory.

The difference between chown Unlike other commands, chgrp allows ordinary users to change the group of a file, as long as the user is a member of that group.

In the UNIX family of systems, file and directory permissions are managed through the owner and the owning group. You can use the chgrp command to change the group ownership of files and directories, specifying either the group name or the group ID.


Syntax



chgrp [-cfhRv][--help][--version][所属群组][文件或目录...]

或

chgrp [-cfhRv][--help][--reference=<参考文件或目录>][--version][文件或目录...]


Parameter Description

The options of the chgrp command can be divided into the following categories: operation flags, output control, recursive processing, and reference files.

Parameters Description
-c or --changes Similar to the "-v" option, but only reports the parts that were changed. Useful when you need to confirm exactly which files were modified.
-f or --quiet or --silent Do not display error messages. Use when a script needs to run silently, to avoid a flood of error output interfering with the results.
-h or --no-dereference Only modify the symbolic link itself, without changing any other related files. Must be used when changing the group of the symbolic link itself.
-R or --recursive Process recursively, handling all files and subdirectories under the specified directory. Use when batch-modifying all files within a directory.
-v or --verbose Display the command execution process. Used for debugging or confirming the modification status of each file.
--help Display help information, listing all available options.
--reference= Set the group ownership of the specified file or directory to be the same as that of the reference file or directory. Used in scenarios where permissions need to be copied in bulk.
--version Display version information.

Example

The following are common examples of the chgrp command, showing how to use it in different scenarios.

Change the group of a single file:



# Set the group of file.txt to developers

chgrp developers file.txt

Change the group of multiple files:



# Bulk change the group ownership of multiple files

chgrp users file1.txt file2.txt file3.txt

Recursively change the group of a directory and its contents:



# -R parameter traverses the entire directory tree

chgrp -R www-data /var/www/html

Use the group from a reference file:



# Set the group of target_file.txt to be the same as source_file.txt

chgrp --reference=source_file.txt target_file.txt

Display detailed operation information:



# -v parameter reports the modification status of each file

chgrp -v staff document.pdf


Practical Application Scenarios

The chgrp command has many application scenarios in real work; the following are common use cases.

Web server file management

Web servers need to run as a specific user or group; setting file group ownership appropriately can control access permissions.

Example

Set the website files' group to the web server group

www-data is the default group for web servers in Debian-based distributions

chgrp

-R

www-data

/

var

/

www

/

html

/

Project collaboration

In team development, you need to set appropriate group permissions on the project directory to ensure team members can access and modify files.

Example

Set the project directory to the development team group

chgrp

-R

developers

/

home

/

projects

/

webapp

/

Also grant write permission to group users

chmod

-R

g+

w

/

home

/

projects

/

webapp

/

Log file management

System logs need specific group permissions so that logging programs can write to them, while ordinary users can only read.

Example

Set log file group permissions

Set the log file group to syslog, allowing the logging daemon to write

chgrp

syslog

/

var

/

log

/

application.log

Backup file organization

Backup files usually need a dedicated group for management, ensuring that only backup administrators can operate on them.

Example

Set the backup files to the backup group

Recursively set the group of the entire backup directory

chgrp

-R

backup

/

backup

/

daily

/


Notes

Tip: If you are unsure of a file's current group, you can use the ls -l command to check. The third column shows the owning user, and the fourth column shows the owning group.

Linux Command Reference Linux Command Reference