chgrp Command

The chgrp command in Linux is used to change the group ownership of files and directories. It allows you to assign a file or directory to a new group.

Syntax

chgrp [OPTION]... GROUP FILE...

Description

Every file and directory in Linux has an owner and a group owner. The chgrp command is specifically for modifying the group ownership. This is important for managing file access permissions within a multi-user environment.

Common uses include:

  • Changing the group of a single file
  • Changing the group of multiple files or directories
  • Recursively changing group ownership for a directory tree

Common Options

Option Description
-c, --changes Like verbose but report only when a change is made
-f, --silent, --quiet Suppress most error messages
-v, --verbose Output a diagnostic for every file processed
-R, --recursive Change files and directories recursively
--from=CURRENT_GROUP Change the group of each file only if its current group is CURRENT_GROUP

Examples

Change group of a file

chgrp developers myfile.txt

Changes the group ownership of myfile.txt to the developers group.

Change group of multiple files

chgrp users file1.txt file2.log

Changes the group ownership of file1.txt and file2.log to the users group.

Change group of a directory and its contents recursively

chgrp -R webmasters /var/www/html

Recursively changes the group ownership of the /var/www/html directory and all its contents to the webmasters group.

Change group only if current group matches

chgrp --from=oldgroup newgroup file.txt

Changes the group of file.txt to newgroup only if its current group is oldgroup.

See also