Search code examples
linuxfile-ownership

How to automatically setup group in subdirectory in Linux


I would like to ensure that in only one particular directory on linux server will have newly-created directory setup particular group?

I.e.: I have directory /data with ownership "user1:global_group" and every new subdirectory should have group ownership the same. Once I create directory by using mkdir /data/subdir1 under user1 or user2 the ownership is "user1:grp_user1" or "user2:grp_user2".

How can I manage the subdirectory ownership?

Many thanks for any ideas ...


Solution

  • You need chmod for that.

    Apply this: chmod g+s directory on a parent directory. Every newly created file and directory, recursively, will have the group of the parent directory.

    So:

    chgrp target_group target_directory
    chmod g+s target_directory
    mkdir -p target_directory/subdirectory/another_one
    ls -l target_directory/subdirectory/another_one 
    

    And observe, how another_one directory has the desired group.