?
Solved

Creating new files with group id of dir

Posted on 1998-02-10
5
Medium Priority
?
302 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

762 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