kazmax - Home Server on Linux

Changing file and directory groups - chgrp

To change the grouping of files and directories, use the "chgrp" command. This page explains how to use the "chgrp" command.

Last Update : October 07, 2020

Changing file and directory groups - chgrp

  1. How to use the chgrp command
  2. Example of using the chgrp command

1. How to use the chgrp command

Usage

chgrp [options] group FILE...

Main Options

Options Meaning
-R, --recursive Recursively change the directory and the group of things contained in it. Execute the command to all files and folders under the directory specified in FILE.
-c, --changes Displays in detail the behavior of the FILE that actually changed the group.

The chgrp command sets the group of the file specified in "FILE" to "GROUP". "GROUP" can be set to a group ID as well as a group name.

2. Example of using the chgrp command

I'll actually use it.

Change the group in test.txt to group2

$ ls -l
total 4
-rw-r--r-- 1 user1 group1 0 Jul 22 08:50 test.txt← The group in test.txt is group1

$ chgrp group2 test.txt
$ ls -l
total 4
-rw-r--r-- 1 user1 group2 0 Jul 22 08:50 test.txt← It's changed to group 2.

Changes the group of files under dir1 to group2.

$ ls -l dir1/
total 12
-rw-r--r-- 1 user1 group1 0 Jul 24 08:32 test1.txt ← The file is grouped by group1
-rw-r--r-- 1 user1 group1 0 Jul 24 08:32 test2.txt ← The file is grouped by group1
-rw-r--r-- 1 user1 group1 0 Jul 24 08:32 test3.txt ← The file is grouped by group1

$ chgrp -R group2 dir1 ←Modify all groups under dir1 with the "-R" option.

$ ls -l dir1/
total 12
-rw-r--r-- 1 user1 group2 0 Jul 24 08:32 test1.txt ← The file group has been changed to group2.
-rw-r--r-- 1 user1 group2 0 Jul 24 08:32 test2.txt ← The file group has been changed to group2.
-rw-r--r-- 1 user1 group2 0 Jul 24 08:32 test3.txt ← The file group has been changed to group2.