The chgrp command stands for change group permission. It is a utility on *nix platforms to change the group ownership of each file to a certain given GROUP.
How to use the chgrp command?
Let’s start with the basic syntax of the chgrp command first and then dive into how to use the tool.
chgrp [OPTION]... GROUP FILE... chgrp [OPTION]... --reference=RFILE FILE...
You can list all the files with their groups using the ls command:
Now if you want to change the group permission of a file, you can do it with chgrp command:
sudo chgrp groupname filename
Similarly you can also change group for a folder by:
sudo chgrp groupname foldername
To change groups of the files and folders (recursively) within use the above command with -R or –recursive
sudo chgrp -R groupname foldername
You can also change group with a reference file instead of specifying a group value, let’s say we have a file called test.txt which is associated with a certain group and there is a folder called ftp/ for which we want to change group to the group associated with test.txt we can do this by:
sudo chgrp -R --reference=referencefile filename
The -v or –verbose would output every diagnostic processed for a file,
The -c or –changes option is very similar to verbose but it only outputs if the changes are made,
The -f or –silent or –quiet is used to suppress the error messages.
Flags for recursion (-R)
There are few flags that are used with recursion (-R) for how the operations travel across, they are as follow:
We’ll test out these options on the following directory tree.
ftp/ ├── remotedir │ └── newdir │ ├── newtest.txt │ └── oldtest.txt ├── sym_web -> /home/suryansh/web └── test.txt
web ├── index.html ├── public_html └── style └── style.css
1. The -h or –no-dereference option will only change the group ownership of the certain symbolic link and not the content inside of that symbolic link.
So as you can see in the image above, using -h flag it actually even changed the flag of the symbolic link and it would not traverse through the symbolic link.
2. The -H option will change the groups of all the files and directories, but not of the symbolic link and the directory and it’s contents of the symbolic link.
As you can see in the above example, the group of symbolic link didn’t change and so the contents inside the directory to which the symbolic link was created.
3. The -L option will change the group of all the files and directories, and when it encounters a symbolic link, it will travel across the symbolic link and change the group of the files and directories within. However, the symbolic link will be left as is!
You can see in the example above, the group was changed for the files and directories within ftp/ but not for the symbolic link ftp/sym_web. But the recursion travelled across the symbolic link to the directory (web/) and changed the groups of the directory (web/) and the contents within it.
4. In the -P flag, the recursion will not travel across the symbolic link. It is the default flag for the -R option.
You can see in the above example, that when we executed -R option with -L flag, the groups were changed for the ftp/ and the contents all the contents inside ftp/ but the recursion didn’t travel across the symbolic link so the groups of web/ and the contents inside web/ were unchanged.
Using chgrp command is very easy, but the recursions can be little bit tricky. We hope you enjoyed this tutorial! 😀