Simple enough. I have user foo. foo belongs to groups foo (main), audio, A, B and common.

Normally files are created with ownership foo.foo.

For a while I would like foo to create files with group common. Basically I see it this way:

Cache present group
Change group to *common*
Touch alpha
Touch beta 
Change group back.

alpha beta ( and anything after ) should be created with owner foo.common. How can I accomplish this?

PS: Oops. Forgot to say that I want to do this in both bash and zsh.

  • Did your file be stored in a common directory?
    – F. Hauri
    Sep 8 '16 at 23:09

Neither bash nor zsh has this functionality.

You can call newgrp. This is an external command, which starts a new process. It runs the program indicated by the SHELL environment variable with no arguments.

newgrp common <<EOS
touch alpha beta
touch belongs_to_original_group

Note that since newgrp starts a new shell, it doesn't inherit from the shell variables of the original shell, only from environment (exported) variables. Any assignments performed there will take effect only there.


To do this, use the newgrp command:

newgrp common
touch whatever
newgrp foo

Since you are a member of both groups (and probably used no password for the groups), you should be able to switch readily. Actually each call to newgrp makes a subshell. You can check "who" you are using id. Whether you exit from a shell (and resume the previous group) or create a new subshell depends on what you want to do.

Unlike some of the related commands, this one is in POSIX:

The newgrp utility shall create a new shell execution environment with a new real and effective group identification.

  • That's half. How do I change back?
    – TLOlczyk
    Sep 8 '16 at 23:25
  • 1
    No, this isn't how newgrp works. It starts a new shell, which will be interactive if stdin is a terminal. touch whatever would be executed when that shell exits. Sep 9 '16 at 1:02
  • @Gilles I guess this depends on whether you use newgrp inside a script or interactively. In the first case you have to actually pass the commands to newgrp via STDIN. But if you are in an interactive shell, newgrp common will just run a new interactive shell and touch whatever will than be typed into the new shell and run with the changed group settings.
    – Adaephon
    Sep 9 '16 at 6:04
  • 1
    @TLOlczyk you can also just leave the interactive shell opened by newgrp common with exit and it will drop you back into the original shell session. Running newgrp foo will actually open another interactive shell, so that you now would have three shells running.
    – Adaephon
    Sep 9 '16 at 6:07

If the files under consideration are not scattered over too many directories, then you can even automate the process by using a subtle feature of Unix permissions, namely the SGID-bit for directories.

Suppose a directory has permissions 2775 (say), which is rwxrwsr-x and is owned by xyz.common. If a user who is member of group common creates a file or subdirectory in this directory, then its group ownership is automatically set to common.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.