Linux : Losing group ownership of files

On the containing folder you'll want to change the group to be dev and then use mark it set-gid.

chgrp dev <containing-folder>
chmod g+ws <containing-folder>

The set gid bit makes files created in that folder to inherit the group of the folder as well as marking the setgid bit on any new folders. You'll want to be careful when moving files into the directory as that will preserve their existing permissions.


You can use the setgid bit on a directory to preserve group ownerships by children.

chown :dev directory/
chmod g+s directory/

You could also mount the filesystem ( assuming ext2/3 ) with the grpid mount option which will make it so whenever you create a new file in a directory, it will make the group owner the same as the parent directory. So then you would just make it so the directory that these files exist in are owned by the group 'dev'.

To remount it if it is the root partition (example):

sudo mount -o remount,grpid,rw,relatime,errors=remount-ro /

From 'man mount 8':

grpid or bsdgroups / nogrpid or sysvgroups
These options define what group id a newly created file gets.

When grpid is set, it takes the group id of the directory in which it is created; otherwise (the default) it takes the fsgid of the current process, unless the directory has the setgid bit set, in which case it takes the gid from the parent directory, and also gets the setgid bit set if it is a directory itself.


Default behavior for emacs is to create the backup file by renaming. From the emacs manual:

Emacs can rename the original file so that it becomes a backup file, and then write the buffer being saved into a new file. After this procedure, any other names (i.e., hard links) of the original file now refer to the backup file. The new file is owned by the user doing the editing, and its group is the default for new files written by the user in that directory.by the user in that directory.

There are several ways to change this.

  • Set the group and sticky bit, as others describe.
  • Run 'newgrp dev' prior to editing the file, so that your default group is dev.

Or emacs specific:

  • Set file-precious-flag in emacs, which changes the behavior to preserve group, but has other side effects.
  • Set backup-by-copying-when-mismatch in emacs, which uses copying rather than renaming when that would cause the owner or group to change.

So, add to your .emacs:

(setq backup-by-copying-when-mismatch 't)

My preference is actually 'newgrp dev', as it is an explicit switch from "personal" mode (the files I edit are just mine), to group dev mode (the files I now edit are shared amongst the group).