chgrp

What is chgrp?

The chgrp command-line tool 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/group by using chown command, and the group by the chgrp command.

How to use chgrp

# syntax : 
chgrp <OPTION> <GROUP> <FILE>
chgrp <OPTION> <--reference=RFILE> <FILE>

##### change the group ownership of a file & folder
chgrp www-data abc.txt # change file group owner
chgrp www-data testdir # change directory group owner

##### change ownership of a folder and all its contents
chgrp -R www-data /var/www/html # change the group of the directory and all its contents in the map /var/www/html

##### use the groupname of a reference file to change the group of another file or folder
chgrp -R --reference=abc.txt /var/www/html # the group that is set on abc.txt will be set on the folder /var/www/html and all of its contents

##### additional options
chgrp -c www-data f1.txt # describe the action for each file whose group changes
chgrp -f www-data f2.txt # supress error messages
chgrp -v www-data f3.txt # verbose, describe every action that is being taken.

##### sym link
chgrp --dereference www-data symbolic_link
chgrp --no-dereference www-data symbolic_link

# checkout the example below for an example on how to reference-derefernce the symlink fil

Last updated