Solved

Creating new files with group id of dir

Posted on 1998-02-10
5
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

688 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