Linux chgrp Command

Linux 命令大全Linux Command Encyclopedia


The Linux chgrp (full English: change group) command is used to change the group ownership of a file or directory.

andchownUnlike some 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 system family, control over file or directory permissions is managed by the owner and the group. You can use the chgrp command to change the group of files and directories, using either the group name or the group ID.


Syntax

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

Parameter Description

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

Parameter Description
-c or --changes Similar to the "-v" parameter, but only reports the changed parts. Suitable for use when you need to confirm which files were actually modified.
-f or --quiet or --silent Does not display error messages. Used when scripts need to execute silently, avoiding large amounts of error output interfering with the results.
-h or --no-dereference Only modifies the symbolic link file itself, without changing any other related files. Must be used when modifying the group of the symbolic link itself.
-R or --recursive Recursively processes all files and subdirectories under the specified directory together. Used for batch modifying all files in a directory.
-v or --verbose Displays the command execution process. Used for debugging or confirming the modification status of each file.
--help Displays help information, listing all available parameters.
--reference=<reference file or directory> Sets the group of the specified file or directory to be the same as that of the reference file or directory. Used in scenarios involving batch copying of permissions.
--version Displays version information.

Examples

The following are common examples of the chgrp command, showing its usage in different scenarios.

Change the group of a single file:

# 将 file.txt 的所属组设置为 developers
chgrp developers file.txt

Change the group of multiple files:

# 一次性修改多个文件的所属群组
chgrp users file1.txt file2.txt file3.txt

Recursively change the group of a directory and its contents:

# -R 参数会遍历整个目录树
chgrp -R www-data /var/www/html

Use the group of a reference file:

# 将 target_file.txt 的组设置为与 source_file.txt 相同
chgrp --reference=source_file.txt target_file.txt

Display detailed operation information:

# -v 参数会报告每个文件的修改状态
chgrp -v staff document.pdf

Practical Application Scenarios

The chgrp command has multiple application scenarios in actual work. The following are common use cases.

Web Server File Management

Web servers need to run as a specific user or group. Properly setting the file group can control access permissions.

Examples

# Set the website file group to the web server group
# www-data is the default group for the Web server in Debian-based distributions
chgrp -R www-data /var/www/html/

Project Collaboration

During team development, it is necessary to set appropriate group permissions for the project directory to ensure team members can access and modify files.

Examples

# 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 require specific group permissions so that log programs can write while ordinary users can only read.

Examples

# Set log file group permissions
# Set the log file group to syslog, allowing the log daemon to write
chgrp syslog /var/log/application.log

Backup File Organization

Backup files usually need a dedicated group to manage them, ensuring that only the backup administrator can operate on them.

Examples

# Set backup files to the backup group
# Recursively set the group of the entire backup directory
chgrp -R backup /backup/daily/

Notes

  • Ordinary users can only change the group of a file to a group they belong to.

  • Be cautious when using the -R parameter to ensure the target directory is the scope you want to modify.

  • When modifying the group of system directories, administrator privileges (using sudo) may be required.

  • A symbolic link itself is also a file. Using the -h parameter can modify the group of the symbolic link without modifying the target file it points to.

Tip: If you are unsure of the current group of a file, 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 命令大全Linux Command Encyclopedia

Other Extensions