emptying a file and keeping its ownership

Mike Balenger msb at hosmsb.ATT.COM
Tue Jan 1 03:25:55 AEST 1991


>>>>> On 31 Dec 90 03:07:23 GMT, ckchee at dgp.toronto.edu (Chuan Chee) said:

ckchee> I have SCO Xenix 2.2.3.  What's the easiest way to "empty" a
ckchee> file while keeping its ownership (owner,group) and access
ckchee> permissions the same?  Actually I only care about permissions
ckchee> (rw-rw-rw).  I would like this done in Bourne shell (or
ckchee> possibly CSH).  One other thing, this shell script is run
ckchee> under root.

ckchee> Here's the way I currently do it:
ckchee>   rm -f $FILE
ckchee>   touch $FILE
ckchee>   chmod +w $FILE
ckchee>   chown $OWNER $FILE
ckchee>   chgrp $GRP $FILE

This works in ksh and sh on SysV.  I guess it should work for other
UNIX look-alike's and shell look-alike's.

    > $FILE

Since you don't remove the file, you don't need to recreate it.  The
file is truncated as a consequence of the defined behavior of output
redirection, but since there is no command, no output is generated
into the file.  An added bonus, this method preserves links.  They are
lost with the "rm" in your original solution.

You must, of course, have write permission to the file to try this.
If that doesn't suite your fancy, try this
    > $FILE && { some machinations on ">", "mv", "cp" & "chmod" }

The assorted machinations will only happen if the redirection fails --
most likely due to lack of write permission.  This probably isn't a
problem if you run a root, but may be useful for less-than-root uses.

Note that the modification time is not changed if the file was already
zero length.  If you need that behavior, add
    touch $FILE



--
----------------------------------------------------------------------
<cute quote>            Michael S. Balenger             (908) 949-8789
<cute disclaimer>       AT&T Bell Labs	           FAX: (908) 949-7512
M_Balenger at att.com      Room 1L-405
msb at hos1cad.att.com     Crawfords Corner Road
att!hos1cad!msb         Holmdel, NJ   07733-1988



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