Solved

Creating new files with group id of dir

Posted on 1998-02-10
5
275 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 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now