The Wayback Machine - https://web.archive.org/web/20240727011403/https://www.geeksforgeeks.org/chgrp-command-in-linux-with-examples/
Open In App

chgrp command in Linux with Examples

Last Updated : 15 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The `chgrp` command in Linux is used to change the group ownership of a file or directory. All files in Linux belong to an owner and a group. You can set the owner by using “chown” command, and the group by the “chgrp” command.

Syntax of `chgrp` command in Linux

chgrp [OPTION]… GROUP FILE…
chgrp [OPTION]… –reference=RFILE FILE…

Note First we need to have administrator permission to add or delete groups. We can login as root for this purpose or use sudo. In order to add a new group, we can use:

sudo addgroup geeksforgeeks

Examples of `chgrp` command in Linux

Changing Group Ownership of a Single File

To change the group ownership of a file.

sudo chgrp geeksforgeeks abc.txt
For a single File

For a single File

Here the group name of the file abc.txt was changed from kcVirtual to geeksforgeeks. Note that when files are created the groupname of the file is the same as the owner under which the file was created.

Changing Group Ownership of Multiple Files

The ‘chgrp’ command can also handle multiple files at once. For instance:

chgrp developers file1.txt file2.txt file3.txt

Here, ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’ will all be assigned to the ‘developers’ group.

Changing Group Ownership of a Directory or Folder

To change the group ownership of a folder.

sudo chgrp geeksforgeeks GFG
For directory or folder

For directory or folder

Recursively change the group ownership of a folder

To recursively change the group ownership of a folder and all of its contents.

sudo chgrp -R geeksforgeeks GFG
Recursively

Recursively

As we can see the group of the folder GFG and its contents F1, F2 was all kcvirtual initially and they were changed to geeksforgeeks with the single command.

Using the groupname of a reference file

Using the groupname of a reference file to change the group of another file or folder.

sudo chgrp -R --reference=abc.txt GFG
reference file

reference file

The groupname of the reference file abc.txt was used to recursively change the group of the folder GFG and all its contents using the –reference option.

Options Available in `chgrp` command in Linux

`-c` or `–changes` Option

To describe the action for each File whose group actually changes.

Example:

sudo chgrp -c geeksforgeeks f1
-c

-c

`-f` Option

To suppress error messages.

Example:

sudo chgrp -f geeksforgeeks f2
-f

-f

`-v` Option

To describe the action or non-action taken for every File.

Example:

sudo chgrp -v geeksforgeeks f1
-v

-v

`–dereference` or `–no-dereference` Option

To change the group name of link files.

Example:

sudo chgrp --dereference geeksforgeeks symbolic_link

--dereference

–dereference

Here file symbolic_link is the link_file for file f1. With “–dereference” option the group name of actual file pointed by symbolic_link gets changed.

Example:

sudo chgrp --dereference geeksforgeeks symbolic_link

Here file symbolic_link is the link_file for file f1. With “–no-dereference” option the group name of the symbolic_link itself gets changed.

Conclusion

In this article we discussed `chgrp` command in Linux which is a powerful tool for changing the group ownership of files and directories. It allows users to modify the group ownership for single or multiple files, as well as entire directories recursively. By using various options, users can efficiently manage access permissions and enhance the security of their file systems. The chgrp command proves to be an essential utility for system administrators and users, providing seamless control over group ownership and access control within the Linux environment.


Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
Article Tags :
three90RightbarBannerImg