You can achieve this by using special permission "setgid bit".
The setgid bit has effect on both files and directories.
In the first case, the file which has the setgid bit set, when executed, instead of running with the privileges of the group of the user who started it, runs with those of the group which owns the file.
When used on a directory, instead, the setgid bit alters the standard behavior so that the group of the files created inside said directory, will not be that of the user who created them, but that of the parent directory itself. This is often used to ease the sharing of files (files will be modifiable by all the users that are part of said group).
If we want to set the setgid bit on a directory use below command.
$ chmod 2775 test_dir
with this above command we gave full privileges on it to it's owner and to the user that are members of the group the directory belongs to plus read and execute permission for all the other users.
We can set this by other way as mentioned below
$ chmod g+s test_dir
Example
We have created multiple users with the name user1, user2, user3:
$ useradd user1
$ useradd user2
$ useradd user3
Also created a group with the name group:
$ groupadd group
Added group as a secondary group in created users:
$ usermod -a -G group user1
$ id user1
uid=1000(user1) gid=1000(user1) groups=1000(user1),1003(group)
$ usermod -a -G group user2
$ id user2
uid=1001(user2) gid=1001(user2) groups=1001(user2),1003(group)
$ usermod -a -G group user3
$ id user3
uid=1002(user3) gid=1002(user3) groups=1002(user3),1003(group)
Created test_dir and added the setgid bit permission:
$ mkdir test_dir
$ chmod 2775 test_dir
$ ls -al
drwxrwsr-x 5 user group 4096 Sep 7 17:10 test_dir
Created files & directories in test_dir using created users:
$ cd test_dir
$ su user1
$ mkdir user1_dir
$ touch user1_file
$ su user2
$ mkdir user2_dir
$ touch user2_file
$ su user3
$ mkdir user3_dir
$ touch user3_file
Here is test_dir directory listing:
$ ls -al
total 44
drwxrwsr-x 5 user group 4096 Sep 7 17:10 .
drwxr-xr-x 1 root root 4096 Sep 7 17:35 ..
drwxrwsr-x 2 user1 group 4096 Sep 7 17:11 user1_dir
-rw-rw-r-- 1 user1 group 0 Sep 7 17:10 user1_file
drwxrwsr-x 2 user2 group 4096 Sep 7 17:09 user2_dir
-rw-rw-r-- 1 user2 group 0 Sep 7 17:09 user2_file
drwxrwsr-x 2 user3 group 4096 Sep 7 17:09 user3_dir
-rw-rw-r-- 1 user3 group 0 Sep 7 17:09 user3_file
Hope this solution works for you.