+3 votes
238 views
How to use CHGRP command in Linux

in Linux / Unix by (552k points)
reopened | 238 views

1 Answer

+4 votes
Best answer

1. How to use chgrp on Linux
2. How to use chgrp to change ownership of a Linux directory
3. Use recursive options with chgrp
4. How to use a reference file with chgrp Linux
5. How to use symbolic links with chgrp Linux

The Linux operating systems administer groups and objects through permits , this allows a user who does not have the permissions simply correct access to the contents of that element, but in addition to this there is another key aspect in the work of objects and ownership of this, logically the owner will have all the power over the object and may or may not grant permissions to other users.

In Linux we have the chgrp command which allows us to change the group ownership of a file or directory in the system either for a collaboration or support work.

Normally this type of permission assignment tasks can be performed with the chown command but chgrp handles a simpler syntax for this task, additionally it is an administrator command , that is, only the root user can change the group of a file or directory determined.
The chgrp usage syntax is as follows:

 chgrp [options] new_group object_name 

Let's see how to use this command in Linux.

1. How to use chgrp on Linux

Step 1


Remember that it is necessary to use sudo for execution, we go to the terminal and there we list the content with its permissions using the following command:

 ls -l 

image

Step 2


We can see, for this example, that the TechnoWikis.txt file is owned by "technowikis". We will
change the property so that it belongs to the group "tests", for this we execute the following:

 sudo chgrp tests TechnoWikis.txt 

image

Step 3


With "ls -l" we can validate that the group has been changed correctly:

image

Step 4


In case it is necessary to change the property of the group for several files simultaneously, we must use wildcards, for example, to change the property of all the .txt files we execute:

 sudo chgrp tests * .txt 

Step 5


It is possible to use the parameter -c (changes - changes), so that the chgrp command lists the changes that have been made, in this case we execute:

 sudo chgrp -c tests * .txt (If there are multiple files) sudo chgrp -c tests TechnoWikis.txt (single object) 

image

2. How to use chgrp to change ownership of a Linux directory

Step 1


With the same mechanism we can change the property of a directory, for example, we will change the property of Downloads, we execute the following:

 sudo chgrp -c tests ./Downloads 

image

Step 2


We check that the owner has been edited with the following command:

 ls -l -d 

image

3. Use recursive options with chgrp


Group ownership can be changed for both files and directories stored in a directory.

Step 1


For this we can resort to the -R (recursive) option, this option allows the chgrp command to modify the group property for all files and subdirectories of the specified directory, we execute:

 sudo chgrp -R tests ./Downloads 

image

Step 2


To check that the change has been effective, we list the permissions of some of the available subdirectories:

 ls -l ./Downloads/TechnoWikis 

image

4. How to use a reference file with chgrp Linux


We can make use of parameters so that the chgrp command modifies the permissions based on an indicated criteria.

Step 1


To demonstrate this we will list the objects with extensions .txt and .deb:

 ls -l * .txt ls -l * .deb 

image

Step 2


We are going to reference that the properties of * .deb are replicated in * .txt:

 sudo chgrp --reference = file.deb file.txt 

image

Step 3


We validate that the property has been replicated:

 ls -l TechnoWikis.txt 

image

5. How to use symbolic links with chgrp Linux


The chgrp command allows us to make use of symbolic links to work, for this it is possible to use chgrp to change the property of the group of symbolic links or of a file to which the symbolic link points.

For this type of case we will use the following syntax:

 ls -l "link" sudo chgrp --dereference "group" link 

It's that simple to use the chgrp command to manage permissions on Linux.


by (3.5m points)

Related questions

+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (552k points) | 330 views
+3 votes
1 answer
asked Nov 9, 2019 in Linux / Unix by backtothefuture (552k points) | 300 views
+5 votes
1 answer
asked Oct 24, 2019 in Linux / Unix by backtothefuture (552k points) | 230 views
+5 votes
1 answer
asked Sep 19, 2019 in Linux / Unix by backtothefuture (552k points) | 310 views
+3 votes
1 answer
asked Sep 17, 2019 in Linux / Unix by backtothefuture (552k points) | 285 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users