emptying a file and keeping its ownership

Kris Stephens [Hail Eris!] krs at uts.amdahl.com
Sat Jan 12 12:20:03 AEST 1991


>I think the easiest way to do this is:
>cp /dev/null <FILE>

That costs a fork/exec of cp...

>>I have SCO Xenix 2.2.3.  What's the easiest way to "empty" a file
>>while keeping its ownership (owner,group) and access permissions the
>>same?  Actually I only care about permissions (rw-rw-rw).
>>I would like this done in Bourne shell (or possibly CSH).
>>One other thing, this shell script is run under root.
>>
>>Here's the way I currently do it:
>>  rm -f $FILE
>>  touch $FILE
>>  chmod +w $FILE
>>  chown $OWNER $FILE
>>  chgrp $GRP $FILE

...and that assumes that the FILE needs to be umask permissions
plus write-permission for everyone, and costs five fork/execs.
It also requires whatever processing preceeded the sample code
to determin OWNER and GRP.  Ideally, that processing would have
included a setting of MODE so that the chmod +w $FILE would be
replaced with a chmod $MODE $FILE to maintain the mode.

What I usually do is

        : > $FILE

at least in a SVR3 system, the permissions and ownerships are
maintained.  (I tested this by: echo a > junk; chmod 666 junk;
chown someone_else junk; : > junk -- the ls -l junk results
were unchanged across the : > junk command.  My umask is 022.)
Execution is controlled based on all applicable file permissions,
and you don't go through a possibly long (from a computer's time
sense) period when the file either doesn't exist at all or exists
with root and root's current group and the wrong mode.

Remember, too, that the datablocks for the file won't actually
be freed until all processes using the file at the outset are
finished.  (Which is to say that if a process has it open on
write, the old file will continue to grow until that other
process is complete and the open-count on the file goes to 0;
only *then* will you gain your disk-space back.)  This is true
regardless of the method applied for zeroing the contents out.

...Kris
-- 
Kristopher Stephens, | (408-746-6047) | krs at uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]



More information about the Comp.unix.xenix.sco mailing list