When the setgid bit (g+s) is set on a directory, any new files or subdirectories created within it should automatically inherit the directory's group rather than the group of the user creating the file. This behavior is useful for shared directories where consistent group ownership is needed (e.g., collaborative environments or web directories).
// Set up directory with setgid
await fs.chmod('/var/www', 0o2775);
await fs.chown('/var/www', 33, 33); // 33 is the default www-data user/gid on unix
// Expected behavior: Files created in '/var/www' inherit group from directory
await fs.writeFile('/var/www/app/index.html', '<html><body>hello</body></html>');
const stat = await fs.stat('/var/www/app/index.html');
console.log(stat.gid); // Should match gid of /var/www
https://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories
Example
When the setgid bit (g+s) is set on a directory, any new files or subdirectories created within it should automatically inherit the directory's group rather than the group of the user creating the file. This behavior is useful for shared directories where consistent group ownership is needed (e.g., collaborative environments or web directories).