Chapter 4. User, Group and Permission

Chgrp (Change Group of File and Directory)

Chgrp (Change Group of File and Directory)
Tag:

The chgrp (CHange GRouP) command is used to change the owner group of files or directories. With the -R option, you can change the owner of all files and directories under the specified directory.

Change the owner group of a file or directory

To change the owner of a single file or directory, you can simply run the chgrp command without any options. For example, to change the owner group of file_a to user_a, run the following command in the directory tree created in the previous section.

Command Line - INPUT
chgrp user_a file_a

Check the owner status of file_a by running the ls -l command. You can see that the file owner group has changed to user_a.

Command Line - INPUT
ls -l file_a
Command Line - RESPONSE
-rw-r--r-- 1 user_a user_a 0 Jan  2 15:31 file_a

Change the owner of an entire directory recursively

To change the owner group of an entire directory including sub-directory and files underneath, you can use the -R option. For example, to change the owner group of the directory tree of dir_a1 to usr_a, run the following command.

Command Line - INPUT
chgrp -R user_a dir_a1

Check the owner group status of dir_a1 and dir_a2 by running the ls -l command. You can see that both directories' owners have changed to user_a.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 8
drwxr-xr-x 3 user_a user_a 4096 Jan  2 15:31 dir_a1
drwxr-xr-x 3 user_a user_a 4096 Jan  2 15:31 dir_b1
-rw-r--r-- 1 user_a user_a    0 Jan  2 15:31 file_a
Command Line - INPUT
ls -l dir_a1
Command Line - RESPONSE
total 4
drwxr-xr-x 2 user_a user_a 4096 Jan  2 15:31 dir_a2

The chgrp (CHange GRouP) command is used to change the owner group of files or directories. With the -R option, you can change the owner of all files and directories under the specified directory.

Change the owner group of a file or directory

To change the owner of a single file or directory, you can simply run the chgrp command without any options. For example, to change the owner group of file_a to user_a, run the following command in the directory tree created in the previous section.

Command Line - INPUT
chgrp user_a file_a

Check the owner status of file_a by running the ls -l command. You can see that the file owner group has changed to user_a.

Command Line - INPUT
ls -l file_a
Command Line - RESPONSE
-rw-r--r-- 1 user_a user_a 0 Jan  2 15:31 file_a

Change the owner of an entire directory recursively

To change the owner group of an entire directory including sub-directory and files underneath, you can use the -R option. For example, to change the owner group of the directory tree of dir_a1 to usr_a, run the following command.

Command Line - INPUT
chgrp -R user_a dir_a1

Check the owner group status of dir_a1 and dir_a2 by running the ls -l command. You can see that both directories' owners have changed to user_a.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 8
drwxr-xr-x 3 user_a user_a 4096 Jan  2 15:31 dir_a1
drwxr-xr-x 3 user_a user_a 4096 Jan  2 15:31 dir_b1
-rw-r--r-- 1 user_a user_a    0 Jan  2 15:31 file_a
Command Line - INPUT
ls -l dir_a1
Command Line - RESPONSE
total 4
drwxr-xr-x 2 user_a user_a 4096 Jan  2 15:31 dir_a2
Tag: