Linux chgrp Command
Learn Linux chgrp Command with examples
Published
- Linux chgrp Command
- chgrp Command Syntax
- chgrp Command to Change the File Group Ownership
- chgrp Command to Group Change Multiple Files
- chgrp Command using Group ID (GID)
- chgrp -f Command
- chgrp -v Command
- chgrp -h Command Change Symlinks Group Ownership
- chgrp -R Command Recursively Change the Group Ownership
- chgrp - -reference Command to Change Group to Match Reference File
- chgrp -c Command to Display chgrp Execution Details
Linux chgrp Command
In Linux, we can use “chgrp” command to change the group ownership of a file or directory.
In Linux file system, each file is associated with an owner and a group and has permissions that determine which users may read, write, or execute the file.
chgrp command Syntax
>> chgrp [OPTIONS] GROUP FILE..
Option | Description |
GROUP | name of the new group; or the group ID (GID). Numeric GID must be prefixed with the + symbol. |
FILE.. | name of one or more files. |
Note: chgrp changes only the group ownership but chown command that allows you to change the user and group ownership
Option | Description |
-f -silent --quiet |
Executes the command without displaying any error messages. |
-v --verbose |
Outputs the action details for every file processed. |
-c --changes |
Similar to --verbose but reports only when making a change. |
--dereference | Affects the referent of each symbolic link; rather than the symbolic link itself. |
-h --no-dereference |
Affects symbolic links instead of any referenced files. Use this option only on systems that can change the ownership of a symlink. |
--no-preserve-root | Does not treat '/' specially (default setting). |
--preserve-root | Fails to operate recursively on '/'. |
--reference=RFILE | Changes a file's group name to the group name of the referenced file. |
-R --recursive | Operates on files and directories recursively. |
-H | If a command line argument is a symbolic link to a directory; traverses it. Used in combination with the -R option. |
-L | In a recursive traversal; traverses every symbolic link to a directory that is encountered. Used in combination with the -R option. |
-P | Does not traverse any symbolic links. This is the default if -H; -L; or -P aren't specified. Used in combination with the -R option. |
--help | Displays the help file and exits. |
--version | Outputs version information and exits. |
chgrp Command to Change the File Group Ownership
To change the group ownership of a file or directory invoke the chgrp command followed by the new group name and the target file as arguments.
For example, to change the group of the file filename to www-data you would run:
>> chgrp www-data filename
chgrp Command to Group Change Multiple Files
You can also pass multiple files as arguments to the chgrp command:
>> chgrp web fruits_1.txt fruits_2.txt directory1
chgrp Command using Group ID (GID)
The numeric group ID (GID) can be used instead of the username. To changes the file’s group ownership to a new group with GID of 1000.
>> chgrp +1000 filename
chgrp -f Command (Hide chgrp Command Errors)
The -f option allows users to suppress potential error messages when executing the chgrp command.
If you run the command with an unprivileged user, you will get an “Operation not permitted” error.
>> chgrp -f web fruits_1.txt
By default, on success, chgrp doesn’t produce any output and returns zero.
chgrp -v Command
Use the -v option to get information about the files that are being processed:
>> chgrp -v web fruits_1.txt fruits_2.txt directory1
chgrp -h Command Change Symlinks Group Ownership
When not operating recursively, the default behavior of the chgrp command is to change the group ownership of the symlink targets, not the symbolic links themselves.
For example, if you try to change the group of the symbolic link symlink1 that points to /opt/file2, chgrp will change the ownership of the file or directory the symlink points to
>> chgrp www-data symlink1
The chances are that instead of changing the target group, you will get a “cannot dereference ‘symlink1’: Permission denied” error.
To change the group ownership of the symlink itself, use the -h option.
>> chgrp -h www-data symlink1
chgrp -R Command Recursively Change the Group Ownership
To recursively change the group ownership of all files and directories under a given directory, use the -R option.
For example, the following command will change the ownership of all files and directories under the /var/www directory to the www-data group:
>> chgrp -R www-data /var/www
When the recursive option is specified chgrp will not traverse the symbolic links and will make no changes to the symlinks. To change the group ownership of the symbolic links, pass the -h option.
>> chgrp -hR www-data /var/www
Other options that can be used when recursively changing the group ownership are -H and -L.
If the argument passed to chgrp command is a symbolic link, the -H option will cause the command to traverse it.
-L tells chgrp to traverse each symlink to a directory that is encountered.
Mostly, you should not use these options because you might mess up your system or create a security risk.
chgrp –reference Command to Change Group to Match Reference File
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 [RFILE_NAME] syntax is the name of the reference file, while [FILE_NAME] is the name of the target file.
For instance, to change the group ownership of the file atom_273.snap to be the same as that of the test file, you would run:
>> sudo chgrp --reference=test matchFile.snap
chgrp -c Command to Display chgrp Execution Details
The -c option allows users to see a list of changes chgrp has made to each file specified.
The list is helpful if you want to make sure the changes are correct. To see the changes applied, use the command:
>> sudo chgrp -c -R [GROUP_NAME] [DIRECTORY/FILE_NAME]
>> sudo chgrp -c -R bosko example