Introduction
In this lab, you will learn how to use the Linux chgrp command to change the group ownership of files and directories. The lab covers understanding the chgrp command, changing the group ownership of a single file, and changing the group ownership of multiple files. The examples provided demonstrate the practical usage of the chgrp command and how it can be used to manage file and directory permissions in a Linux environment.
Understand the chgrp Command
In this step, we will learn about the chgrp command in Linux, which is used to change the group ownership of files and directories.
The chgrp command has the following syntax:
chgrp [options] GROUP FILE(S)
Here, GROUP is the name of the group you want to assign to the file(s), and FILE(S) are the file(s) or directory(ies) whose group ownership you want to change.
Some common options for the chgrp command include:
-R: Recursively change the group ownership of files and directories in a directory tree.-v: Display a message for each file whose group is being changed.-c: Like verbose, but report only when a change is made.-f: Suppress most error messages.
Let's start by creating a new file and a directory in the ~/project directory:
cd ~/project
touch file.txt
mkdir mydir
Now, let's change the group ownership of the file.txt file to the admin group:
sudo chgrp admin file.txt
Example output:
In this example, we used the sudo command to execute the chgrp command with the admin group as the new group owner for the file.txt file.
You can also change the group ownership of multiple files or directories at once:
sudo chgrp admin file.txt mydir
Example output:
Here, we changed the group ownership of both the file.txt file and the mydir directory to the admin group.
To verify the group ownership of the files, you can use the ls -l command:
ls -l
Example output:
The output shows that the group ownership of the file.txt file and the mydir directory has been changed to the admin group.
Change the Group Ownership of a File
In this step, we will learn how to change the group ownership of a file using the chgrp command.
Let's start by creating a new file in the ~/project directory:
cd ~/project
touch myfile.txt
Now, let's check the current group ownership of the myfile.txt file:
ls -l myfile.txt
Example output:
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 myfile.txt
As you can see, the group ownership of the myfile.txt file is set to the labex group, which is the default group for the labex user.
To change the group ownership of the myfile.txt file to the admin group, we can use the chgrp command:
sudo chgrp admin myfile.txt
Example output:
Now, let's verify the group ownership of the myfile.txt file:
ls -l myfile.txt
Example output:
-rw-r--r-- 1 labex admin 0 Apr 12 12:34 myfile.txt
The output shows that the group ownership of the myfile.txt file has been changed to the admin group.
Change the Group Ownership of Multiple Files
In this step, we will learn how to change the group ownership of multiple files using the chgrp command.
Let's start by creating two new files in the ~/project directory:
cd ~/project
touch file1.txt file2.txt
Now, let's check the current group ownership of the files:
ls -l file1.txt file2.txt
Example output:
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
As you can see, the group ownership of both file1.txt and file2.txt is set to the labex group.
To change the group ownership of both files to the admin group, we can use the chgrp command with multiple file arguments:
sudo chgrp admin file1.txt file2.txt
Example output:
Now, let's verify the group ownership of the files:
ls -l file1.txt file2.txt
Example output:
-rw-r--r-- 1 labex admin 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex admin 0 Apr 12 12:34 file2.txt
The output shows that the group ownership of both file1.txt and file2.txt has been changed to the admin group.
You can also use the -R option to recursively change the group ownership of all files and directories in a directory tree:
sudo chgrp -R admin ~/project
This command will change the group ownership of all files and directories in the ~/project directory and its subdirectories to the admin group.
Summary
In this lab, we learned about the chgrp command in Linux, which is used to change the group ownership of files and directories. We started by understanding the syntax and common options of the chgrp command, and then practiced changing the group ownership of a single file and multiple files/directories. We also learned how to verify the group ownership of files using the ls -l command. The key takeaways from this lab are the usage of the chgrp command and the ability to change the group ownership of files and directories in a Linux environment.



