Solved

Creating new files with group id of dir

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

813 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

10 Experts available now in Live!

Get 1:1 Help Now