Ensuring new files in a directory belong to the group

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.


Set the setgid permission flag on the folders.

chmod g+s dirname

This might get a few people stuck with setgid, if the folder's group is different from your own you may need to run chmod as root but you won't get any error indicating you need to do this.

without sudo

$ ls -ld dir
drwxrwxr-x 2 luke testgroup 4096 Mar  9 10:44 dir

$ chmod g+s dir                                     # no errors

$ ls -ld dir
drwxrwxr-x 2 luke testgroup 4096 Mar  9 10:44 dir   # but nothing changed

$ touch dir/nosudo && ls -l dir/
-rw-rw-r-- 1 luke luke 0 Mar  9 10:51 nosudo        # and the group is set wrong

with sudo

$ sudo chmod g+s dir

$ ls -ld dir
drwxrwsr-x 2 luke testgroup 4096 Mar  9 10:44 dir   # the setgid bit is now on

$ touch dir/withsudo && ls -l dir/
-rw-rw-r-- 1 luke luke      0 Mar  9 10:51 nosudo
-rw-rw-r-- 1 luke testgroup 0 Mar  9 10:51 withsudo # and the group is set right