Introduction
The chgrp command changes ownership of a directory or file on Linux. Each file is associated with an owner user or group and has rules that define which users or groups can read, write, or execute the file.
In this tutorial, you will learn how to use the chgrp command to change the ownership of a filegroup.
Prerequisites
A
- system running Linux
- A user account with sudo privileges Access
- to a terminal (Ctrl+Alt+T
)What is the chgrp command
? The
chgrp (Change Group) command alters the name of the group to which a file or directory belongs. Each file in Linux is created by a user, while each user belongs to groups. When you change the group ownership of a file, you also change the permissions to access and modify a file.
Because the chgrp command requires superuser permissions, remember to run the command with the sudo prefix for successful execution.
Explanation of chgrp command syntax
The chgrp command syntax is: chgrp
[OPTION] [GROUP_NAME] [DIRECTORY/FILE_NAME] [OPTION]
- – Additional options. You can use the command without them.
- [GROUP_NAME] – The group to which you want to assign the file.
- [DIRECTORY/FILE_NAME] – The destination directory/file.
The [OPTIONS] are:
The [GROUP_NAME] attribute is the new group that the file or directory acquires when it is executed. You can also specify the group ID (GID) instead of the group name by adding it with the + sign.
[DIRECTORY/FILE_NAME] is the destination directory or file name whose group affiliation you want to change.You can check which group a file or directory belongs to by running the following command
: ls -l
The output lists the group property as in the following image:
In this output, the ls command displays the details of each file and subdirectory contained in the phoenixNAP directory. The owner and group of every file and directory here is bosko.
chgrp command
: 5
examples
Take a look at five examples of how to use the chgrp command. Below are sample commands and results for changing the ownership of the directory group, changing the ownership of the group to match a reference file, showing execution details, and hiding command errors.
Change
directory group
If you want to change the ownership of
a directory group, place the directory name instead of the [FILE_NAME] attribute in the command: chgrp [GROUP_NAME] [DIRECTORY_NAME]
Take a look at the group name in the directory example before and after using the chgrp command. The directory group was changed from the default bosko to phoenixnap using the command:
sudo chgrp phoenixnap example Recursively change group ownership The -R option allows you to
recursively change
the group ownership of a directory, any of its subdirectories, and all the content it contains. The syntax is
: sudo chgrp -R [GROUP_NAME] [DIRECTORY_NAME] For example, the
following command changes the group affiliation of the sample directory and all of its files:
sudo chgrp -R phoenixnap example
In the image above, you’ll see that files within a sample subdirectory also have ownership of the phoenixnap group.
Change group to match
the file
of reference If you want to change the group of a file to Match the group of
another reference file, use the following syntax: chgrp [OPTION] -reference=[RFILE_NAME] [FILE_NAME] The syntax [RFILE_NAME] is the name of the
reference file, while [FILE_NAME] is the name of the
destination file. For example, to change the group property of the atom_273.snap file to
be the same as that of the test file, you would run: sudo chgrp -reference=test atom_273.snap
Show chgrp
execution details
The -c option allows users to see a list of changes that chgrp has performed on each specified file. The list is useful if you want to make sure your changes are correct. To view the applied
changes, use the command: sudo chgrp -c -R [GROUP_NAME] [DIRECTORY/FILE_NAME] To list the changes that occurred in our example directory, we would run: sudo chgrp -c -R bosko example
In this case, we change the sample directory group and all its files that contain it, and chgrp lists all changes as they occurred. Hide chgrp command errors
The -f command allows users to suppress possible error messages when running the chgrp
command. The syntax to hide command errors
is: sudo chgrp -f [GROUP_NAME] [DIRECTORY/FILE_NAME] For example, here we use the chgrp command on a non-existent file: sudo
chgrp
phoenixnap non-existent file
The result shows that you cannot access the file because no such file or directory exists.
However, running the same command with the -f option suppresses this error:
Conclusion
Now you know how to use the chgrp command to change the group ownership of a file or directory. You also know how to customize the process using the available options.
Find other useful commands in our Linux command cheat sheet.