7

I have a folder hierarchy that I want to change the group ownership including any subsequent file/folder additions. I was going to use chmod g+s but I'm not sure if it applies to already created folders and how I run that with the group name.

mike628
  • 309
  • 2
  • 5
  • 10
  • What do you want: change the group ownership or set SGID bit for this folder and all subdirectories, or both? Could you please explain your situation a little more? – quanta Sep 01 '11 at 03:01

4 Answers4

12

Using rthomson's answer/command:

find /path/to/hierarchy -type d | xargs chmod g+s

It gave me problems when there are spaces in any of the sub-directories: Instead, I find just using the find -exec options much easier, i.e.:

find /path/to/hierarchy -type d -exec chmod g+s {} \;

lsiu
  • 221
  • 2
  • 6
  • Are you attempting to clarify your question? If so, please *Edit* the question. If this is an attempt to "Answer", it's not abundantly clear that the `find...-exec` method solves the problem... – Signal15 Dec 20 '14 at 03:02
  • I have updated my answer to clarify why use `find ... -exec` instead. – lsiu Feb 07 '15 at 03:25
  • 1
    It really is a problem, that there is no such thing as an uppercase `S` for chmod (like the uppercase `X` [affecting directories only..](https://unix.stackexchange.com/a/416885/156470))... thus good to have this method of setting it for directories only. – Frank N Dec 27 '20 at 08:09
7

The (very slightly) expand on the existing answers, you'll probably want to both recursively set the ownership on any existing files and directories and the setgid bit on any existing directories. That is, if your hierarchy already has existing files and directories. If it doesn't, you don't need to worry about the recursive part.

Something like this:

find /path/to/hierarchy -type d | xargs chmod g+s
chgrp -R groupname /path/to/hierarchy

and you're set. Now if you want to ensure certain rwx permissions on files/dirs copied or moved into the hierarchy, that's a bit trickier. You'll likely need to use default ACLs but the Linux ACL implementation (based on a dead POSIX proposal, I believe) doesn't always work as one might expect.

rthomson
  • 1,059
  • 9
  • 14
2

you can use

chgrp -R folder

That will change the group owner recusively in folder , and sub-folders, and their respective files

2

g+s adds the "setgid" bit which basically only affects the default behavior of creating new files. (in short... any new files/directories created will have the group set to the group of the parent folder)

you could simply

chgrp group-name some_directory/* -R

to change the group of the files under "some_directory" to "group-name" recursively (-R)

TheCompWiz
  • 7,409
  • 17
  • 23
  • If he actually is wanting change the permissions and not ownership then he should add a `-R` to the `chmod g+s` – Zoredache Aug 31 '11 at 20:05
  • He said in his post he wants to change group ownership. Not the permissions. – TheCompWiz Aug 31 '11 at 20:08
  • 1
    I know what he said, but sometimes people don't say what they mean, or they are not quite sure what they mean. Given that he was looking at chmod, I am tempted to believe that he might actually be talking about permissions. – Zoredache Aug 31 '11 at 20:48