?
Solved

Creating new files with group id of dir

Posted on 1998-02-10
5
Medium Priority
?
312 Views
Last Modified: 2013-12-06
How do you create a file in a directory in such
a way that the file gets the same group id as the
directory?

Example:
(hal:~/tmp): ls -la
total 2
drwxrwxr-x   2 mag      foobar       1024 Feb 10 14:22 .
drwxrwxr-x   3 mag      mag          1024 Feb 10 14:22 ..

(hal:~/tmp): touch foo
(hal:~/tmp): ls -la
total 2
drwxrwxr-x   2 mag      foobar       1024 Feb 10 14:22 .
drwxrwxr-x   3 mag      mag          1024 Feb 10 14:22 ..
-rw-rw-r--   1 mag      mag             0 Feb 10 14:22 foo

This is what happens, but I want the group to be "foobar"
not "mag".

That is, I want:
(hal:~/tmp): ls -la
total 2
drwxrwxr-x   2 mag      foobar       1024 Feb 10 14:22 .
drwxrwxr-x   3 mag      mag          1024 Feb 10 14:22 ..
-rw-rw-r--   1 mag      foobar          0 Feb 10 14:22 foo

So how do I get this to happen WITHOUT type chgrp after the file has been created?

Thank You!
0
Comment
Question by:mag062397
5 Comments
 
LVL 5

Expert Comment

by:n0thing
ID: 2008786
You either have to ask your admin to change your default group
ID to be foobar in the /etc/passwd file or just create a script
like:
#!/bin/sh
touch $1
chgrp foobar $1

So if you named that script "create_foobar" by running
create_foobar foo, it will create a file called foo with the group id of foobar.

Regards,
Minh Lai
0
 

Author Comment

by:mag062397
ID: 2008787
Okay, sorry, I should be more clear.

1) I don't want to change the default group.  I want someone to be in several groups and still be able to create files with the current directories group id.

2) The purpose of "touch" was just to make the example simple. I want group id to be set correctly for any file that gets created in the directory.

Is there no way to do this?

0
 
LVL 3

Accepted Solution

by:
dhm earned 600 total points
ID: 2008788
On many versions of unix, if the directory has the setgid bit set, then any files created in the directory will be created with the group ID set the same as the directory's group.  For example, here's an excerpt from a session on a Solaris-2.5.1 machine:

dhm@sweet[71]$ ls -l
total 16
drwxrwxr-x   2 dhm      eng            69 Feb 10 16:25 dir1/
drwxrwsr-x   2 dhm      sys            69 Feb 10 16:25 dir2/

[Note that dir2 has the setgid bit set, and that its group is sys, rather than eng (my default group)]

dhm@sweet[72]$ touch dir1/foo
dhm@sweet[73]$ touch dir2/foo
dhm@sweet[74]$ ls -l dir1
total 0
-rw-rw-r--   1 dhm      eng             0 Feb 10 16:26 foo
dhm@sweet[75]$ ls -l dir2
total 0
-rw-rw-r--   1 dhm      sys             0 Feb 10 16:26 foo

The file created in dir1 has my default group, but the file created in dir2 has the same group as the directory.  This works even if the group of dir2 is one that the user has no connection with whatsoever (but, of course, the directory must be world-writable for the user to be able to create a file there).
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008789
dhm's solution works on Linux, IRIX, HP-UX and AIX too.
Keep in mind that the s-bit for the group must be set.
0
 

Author Comment

by:mag062397
ID: 2008790
Thank you.  I'm using Linux and Solaris 2.5.1 too.  It
worked perfect on both.  It's nice to finally know how
to do this--something I should have asked along time ago.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question