Linux – Ensuring new files in a directory belong to the group


I want to create a shared directory when a number of users (all belong to say mygroup) can create and edit files. I would like all files in this directory and subdirectory to belong to mygroup

I have changed existing files to have group mygroup using chgrp, but new files still get created belong to the user's primary group. Is there a way of ensuring new files belong to the group without repeatedly running chgrp.

Best Answer

You want to set the SetGID bit.

chmod g+s dir

All new files created in the directory will have the group set to the group of the directory.

A superuser blog post explained the sticky bits and other Linux permission bits:

SetGID, however, is a whole different ball game. When a directory has the SetGID bit set and a file is created within that directory the group ownership of the file is automatically modified to be the group of the directory.