[comp.unix.xenix.sco] emptying a file and keeping its ownership

ckchee@dgp.toronto.edu (Chuan Chee) (12/31/90)

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

Thanks in advance.

...Chuan Chee
   ckchee@dgp.utoronto.ca
   ckchee@dgp.toronto.edu (internet)
   ckchee@dgp.utoronto    (bitnet)

glen@sungate.UUCP (Glen Barney) (12/31/90)

Try -     cat /dev/null > nameoffile

Hope this helps!
-- 
--*--  --*--  --*--  --*--  --*--  --*--  --*--  --*--  --*--  --*--  --*--
Glen / sungate! 619-453-6644         It'll take time, but we're going far...
BangPath: uunet!sungate!glen         you and me, I know we are... In time,
InterNet: glen%sungate@uunet.UU.NET  we'll be dancing in the streets all night

karish@mindcraft.com (Chuck Karish) (12/31/90)

In article <1990Dec30.220722.29050@jarvis.csri.toronto.edu>
ckchee@dgp.toronto.edu (Chuan Chee) writes:
|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).

  > $FILE

A redirect with no input truncates the file under Bourne shell.
-- 

	Chuck Karish		karish@mindcraft.com
	Mindcraft, Inc.		(415) 323-9000		

donlash@uncle.uucp (Donald Lashomb) (12/31/90)

In article <1990Dec30.220722.29050@jarvis.csri.toronto.edu> ckchee@dgp.toronto.edu (Chuan Chee) writes:
=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.

This works for me:
	> file      # shell opens file for writing, and writes 0 bytes

hope this is what you are looking for
-Don

msb@hosmsb.ATT.COM (Mike Balenger) (01/01/91)

>>>>> On 31 Dec 90 03:07:23 GMT, ckchee@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@att.com      Room 1L-405
msb@hos1cad.att.com     Crawfords Corner Road
att!hos1cad!msb         Holmdel, NJ   07733-1988

segrove@PacBell.COM (S. E. Grove) (01/01/91)

Summary: > $FILE will do the job. 



        Stephen Grove  Comm. Tech. ESS Pacific Bell
	segrove@pbhya.PacBell.COM
	PacBell.COM!{rtpkh0,rtpkh1}!segrove
-- 
        Stephen Grove  Comm. Tech. ESS Pacific Bell
	seg@pbhya.PacBell.COM
	segrove@pbhya.PacBell.COM (After 12-14-90)
	PacBell.COM!{rtpkh0,rtpkh1}!segrove

dberg@informix.com (David I. Berg) (01/01/91)

> 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? 

How about:
	cat /dev/null >! $FILE
  ___                   ___              dberg@cougar.informix.com
  /  ) __      . __/    /_ ) _       __  Informix Software Inc.  (303) 850-0210
_/__/ (_(_ (/ / (_(_  _/__> (-' -/~ (_-  5299 DTC Blvd #740; Englewood CO 80111
{uunet|pyramid}!infmx!dberg
The opinions expressed herein do not necessarily reflect those of Informix Inc.

john@cstreet.com (John Poplett) (01/01/91)

In article <1990Dec30.220722.29050@jarvis.csri.toronto.edu> ckchee@dgp.toronto.edu (Chuan Chee) writes:
>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.
>

The simplest way to truncate a file using the Bourne shell
is:

> file

Here's a short Bourne shell script that truncates files. Cut it
out and save it to trunc.sh and run "make trunc" to get an executable
script file.

John

------ cut here -------- cut here -------- cut here --------
: 
# @(#)trunc.sh -- truncate or create empty files. Optionally, setting 
# owner or group ID.

usage()
{
	echo "usage: $0 [-o owner] [-g group] file1 [file2...]" 1>&2
	exit 1
}

if [ $# -lt 1 ]
then
	usage
fi

set -- `getopt g:o: $*`

if [ $? != 0 ]
then
	usage
fi

for i in	$*
do
	case $i in
	-g)	      group=$2;	shift; shift;;
	-o)	      owner=$2;	shift; shift;;
	--)       shift; break;;
	esac
done

for file in $*
do
	> $file
done

if [ $group ]
then
	chgrp $group $*
fi

if [ $owner ]
then
	chown $owner $*
fi

exit 0
-- 
John Poplett @ C Street Software     | Never make forecasts, especially
312 Wolff St. Oxnard, Ca. 93033 USA  | about the future.
(805) 486-7807 / john@cstreet.com    |  ~ Sam Goldwyn

jik@athena.mit.edu (Jonathan I. Kamens) (01/01/91)

  Several people have suggested using

    > $FILE

to truncate a file while retaining its old permissions.  This will work under
sh or ksh or bash or other shells that allow null commands for redirection. 
Unfortunately, csh and tcsh do not allow such null commands :-(.  Therefore,
I'd like to point out another way to do this that works on most platforms I've
seen.

  From the man page for cp(1):

    DESCRIPTION
         File1 is copied onto file2.  By default, the mode and owner
         of file2 are preserved if it already existed; otherwise the
         mode of the source file modified by the current umask(2) is
         used.

Therefore, you can use

    cp /dev/null $FILE

to truncate a file and save its current permissions.

  I hope this helps.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (01/01/91)

As quoted from <1990Dec31.214030.7816@athena.mit.edu> by jik@athena.mit.edu (Jonathan I. Kamens):
+---------------
|   Several people have suggested using
|     > $FILE
| to truncate a file while retaining its old permissions.  This will work under
| sh or ksh or bash or other shells that allow null commands for redirection. 
| Unfortunately, csh and tcsh do not allow such null commands :-(.  Therefore,
+---------------

	% ls -s file
	4 file
	% echo > file
	% ls -s file
	0 file
	% _

Not that I like csh's echo behavior in this case, but it does get around the
"null command" problem.

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

jpr@jpradley.jpr.com (Jean-Pierre Radley) (01/02/91)

In article <1991Jan1.040621.27634@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
>As quoted from <1990Dec31.214030.7816@athena.mit.edu> by jik@athena.mit.edu (Jonathan I. Kamens):
>+---------------
>|   Several people have suggested using
>|     > $FILE
>| to truncate a file while retaining its old permissions.  This will work under
>| sh or ksh or bash or other shells that allow null commands for redirection. 
>| Unfortunately, csh and tcsh do not allow such null commands :-(.  Therefore,
>+---------------
>
>	% ls -s file
>	4 file
>	% echo > file
>	% ls -s file
>	0 file
>	% _
>
>Not that I like csh's echo behavior in this case, but it does get around the
>"null command" problem.


I find that in my csh, the following works:
	% : > file
That initial colon does the trick. What mechanism is operating here?

 Jean-Pierre Radley	    NYC Public Unix	jpr@jpr.com	CIS: 72160,1341

shwake@raysnec.UUCP (Ray Shwake) (01/02/91)

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) writes:
>	% echo > file
>	% ls -s file
>	0 file

	Not quite. On the systems I know, echo outputs a new line character.
Thus your ostensibly zero'ed file would have a size of 1 byte.

jfh@rpp386.cactus.org (John F Haugh II) (01/03/91)

In article <187@raysnec.UUCP> shwake@raysnec.UUCP (Ray Shwake) writes:
>allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) writes:
>>	% echo > file
>>	% ls -s file
>>	0 file
>
>	Not quite. On the systems I know, echo outputs a new line character.
>Thus your ostensibly zero'ed file would have a size of 1 byte.

Yes, quite ;-).  The C shell has a builtin echo function which does
exactly what Brandon showed.  Watch -
--
Script is typescript, started Thu Jan  3 08:17:12 1991
rpp386-> /bin/csh
1 - rpp386-> echo > file
2 - rpp386-> ls -l file
-rw-r-----   1 jfh      root           0 Jan  3 08:17 file
3 - rpp386-> exit
4 - rpp386-> rpp386-> 
Script done Thu Jan  3 08:17:31 1991
--
As you can see "file" has a zero length.
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org
"While you are here, your wives and girlfriends are dating handsome American
 movie and TV stars. Stars like Tom Selleck, Bruce Willis, and Bart Simpson."

jay@gdx.UUCP (Jay A. Snyder) (01/05/91)

>
>I find that in my csh, the following works:
>	% : > file
>That initial colon does the trick. What mechanism is operating here?
>

The ':' is a comment character for old versions of sh (dating from
V7), in fact V7 bourne shell doesn't accept '#' for comments.  Most
modern verions of sh do recongnize the ':'.

If you are running Xenix, the ':' is also used to tell a non bourne
shell that a script is intended for bourne shell (equiv to a BSD file
with #!/bin/sh as the first line).

J

jpr@jpradley.jpr.com (Jean-Pierre Radley) (01/06/91)

In article <97@gdx.UUCP> jay@gdx.UUCP (Jay A. Snyder) writes:
>>
>>I find that in my csh, the following works:
>>	% : > file
>>That initial colon does the trick. What mechanism is operating here?
>>
>
>The ':' is a comment character for old versions of sh (dating from
>V7), in fact V7 bourne shell doesn't accept '#' for comments.  Most
>modern verions of sh do recongnize the ':'.
>
>If you are running Xenix, the ':' is also used to tell a non bourne
>shell that a script is intended for bourne shell (equiv to a BSD file
>with #!/bin/sh as the first line).

I knew that, thanks, but this is out of csh on either SCO Xenix or SCO Unix.
And I'm typing that at the command line, not in a script.

Are you saying that typing ':' at a csh prompt calls sh? I doubt it, because 
if I try ": command", I don't get "command" to run.

 Jean-Pierre Radley	    NYC Public Unix	jpr@jpr.com	CIS: 72160,1341

jak@sactoh0.SAC.CA.US (Jay A. Konigsberg) (01/06/91)

In article <187@raysnec.UUCP> shwake@raysnec.UUCP (Ray Shwake) writes:
>allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) writes:
>>	% echo > file
>>	% ls -s file
>>	0 file
>
>	Not quite. On the systems I know, echo outputs a new line character.
>Thus your ostensibly zero'ed file would have a size of 1 byte.

Well, I can think of three solutions:

one:
    $ echo "\c" > file

two:
    $ cp /dev/null file  # or /dev/zero

three:
    $ > file

Choose ...

-- 
-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
If something is worth doing, it's worth doing correctly.

mjohnsto@admin8780.shearson.com (Mike Johnston) (01/08/91)

Isn't it silly to spend all this time developing rediculously long solutions
to a simple problem when:


touch $FILENAME


would suffice?
--
Michael R. Johnston	mjohnsto@shearson.com || mjohnstonn@mcimail.com
System Administrator		UUCP:     uunet!slcpi!mjohnsto
Lehman Brothers Inc.		Phone:    (212) 640-9116
                  "Unix Uber Alles!"

--
--
Michael R. Johnston	mjohnsto@shearson.com || mjohnstonn@mcimail.com
System Administrator		UUCP:     uunet!slcpi!mjohnsto
Lehman Brothers Inc.		Phone:    (212) 640-9116
                  "Unix Uber Alles!"

fyl@ssc.UUCP (Phil Hughes) (01/08/91)

In article <97@gdx.UUCP>, jay@gdx.UUCP (Jay A. Snyder) writes:
> >
> >I find that in my csh, the following works:
> >	% : > file
> >That initial colon does the trick. What mechanism is operating here?

> The ':' is a comment character for old versions of sh (dating from
> V7), in fact V7 bourne shell doesn't accept '#' for comments.  Most
> modern verions of sh do recongnize the ':'.

> If you are running Xenix, the ':' is also used to tell a non bourne
> shell that a script is intended for bourne shell (equiv to a BSD file
> with #!/bin/sh as the first line).

Well, close.  The : command is a null command.  It returns 0 exit status.
Sort of like true and, in fact, I expect it predates true.  But, it
isn't a comment.  This is why the proposed command works.  There is a
command but the only thing it does is return a 0 exit status.

Comments were added later and # is the comment character.  Prior to
the existence of #, people would use : to introduce a comment, the
most common one being the program name on the first line of a script.
When C shell was born, there needed to be a way to tell it whether it was
to interpret a script itself or pass it off to the Bourne shell.  As many
Bourne shell scripts already existed a method was needed that would not
break existing scripts.  So the # comment was born and it is required
to be the first character of a C shell script.  If it is not (as would
be the case will all existing Bourne shell scripts) it would be
passed off to the Bourne shell.

The reason a line with just
    > file
works in Bourne shell is the shell performs all the I/O redirection
and then looks for the command.  There is none so it just terminates and
is happy.  C shell apparently cares.

-- 
Phil Hughes, SSC, Inc. P.O. Box 55549, Seattle, WA 98155  (206)FOR-UNIX
     uunet!pilchuck!ssc!fyl or attmail!ssc!fyl            (206)527-3385

perry@bluemtn (Perry Minyard (3MTA3)) (01/08/91)

S
Reply-To: perry@bluemtn.UUCP (Perry Minyard (3MTA3))
Followup-To: 
Distribution: na
Organization: blue mountain software
Keywords: 

In article <MJOHNSTO.91Jan7113152@admin8780.shearson.com> mjohnsto@admin8780.shearson.com (Mike Johnston) writes:
>
>Isn't it silly to spend all this time developing rediculously long solutions
>to a simple problem when:
>
>
>would suffice?

No, it wouldn't.  touch will not erase a file that already exists.   touch
only updates the time/date stamp on the file.  If the file doesn't exist,
it will create it.    <at least this holds true for SCO XENIX>
>
>--
>Michael R. Johnston	mjohnsto@shearson.com || mjohnstonn@mcimail.com
>System Administrator		UUCP:     uunet!slcpi!mjohnsto
>Lehman Brothers Inc.		Phone:    (212) 640-9116
>                  "Unix Uber Alles!"
>


-- 
-------------------------------------------------------------------------------
                               | Perry Minyard
"A mind is a terrible thing    | ..!emory!bluemtn!perry              (UUCP)
 to taste." - Ministry         | perry@bluemtn.atl.ga.us             (Internet)

ghong@sdcc13.ucsd.edu (Gary) (01/09/91)

In article <1991Jan8.124644.6514@bluemtn> perry@bluemtn (Perry Minyard (3MTA3)) writes:
>S
>Reply-To: perry@bluemtn.UUCP (Perry Minyard (3MTA3))
>Followup-To: 
>Distribution: na
>Organization: blue mountain software
>Keywords: 
>
>In article <MJOHNSTO.91Jan7113152@admin8780.shearson.com> mjohnsto@admin8780.shearson.com (Mike Johnston) writes:
>>
>>Isn't it silly to spend all this time developing rediculously long solutions
>>to a simple problem when:
>>
>>
>>would suffice?
>
>No, it wouldn't.  touch will not erase a file that already exists.   touch
>only updates the time/date stamp on the file.  If the file doesn't exist,
>it will create it.    <at least this holds true for SCO XENIX>


Ok, you are right about that, but touch will give you an empty file
if file does not exists.

Here is another simple solution to emptying a file and keeping its
ownership:

% cat > <filename>
^D

Again, <filename> is the name of the file.  If your NOCLOBBER
variable is set, you probably want to use:

% cat >! <filename>
^D

I'm not sure, but you may/may not need a space before the bang ("!").

Gary Hong
ghong@ucsd.edu

mike@bria.UUCP (Michael Stefanik) (01/09/91)

In article <MJOHNSTO.91Jan7113152@admin8780.shearson.com> admin8780.shearson.com!mjohnsto (Mike Johnston) writes:
:
:Isn't it silly to spend all this time developing rediculously long solutions
:to a simple problem when:
:
:
:touch $FILENAME
:
:
:would suffice?

Bzzzzt.  Touch only creates a file if it doesn't already exist; it certainly
does NOT empty files.  Normally, it'll just change the modification
timestamp for the file.
-- 
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
--
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."

mjohnsto@admin8780.shearson.com (Mike Johnston) (01/10/91)

How about just:

cat /dev/null > $FILENAME

Doesn't this satisfy the requirments?



--
--
Michael R. Johnston	mjohnsto@shearson.com || mjohnstonn@mcimail.com
System Administrator		UUCP:     uunet!slcpi!mjohnsto
Lehman Brothers Inc.		Phone:    (212) 640-9116
                  "Unix Uber Alles!"

speelmo@bronze.ucs.indiana.edu (Lance Speelmon -- UCS) (01/11/91)

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

Lance

In article <1990Dec30.220722.29050@jarvis.csri.toronto.edu> ckchee@dgp.toronto.edu (Chuan Chee) writes:
>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
>
-- 
==============================================================================
| Lance Speelmon                      |  University Computing Services       | 
| speelmo@bronze.ucs.indiana.edu      |  Network Operations Center           |
==============================================================================

boyd@necisa.ho.necisa.oz.au (Boyd Roberts) (01/11/91)

In article <MJOHNSTO.91Jan9134450@admin8780.shearson.com> mjohnsto@admin8780.shearson.com (Mike Johnston) writes:
>
>cat /dev/null > $FILENAME
>

Ever use the Bourne shell?

  > file


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

]) (01/12/91)

>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@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. ]

msb@hosmsb.ATT.COM (Mike Balenger) (01/16/91)

>>>>> On 12 Jan 91 01:20:03 GMT, krs@uts.amdahl.com (Kris Stephens [Hail Eris!]) said:

krs> What I usually do is

krs>         : > $FILE

The ":" doesn't seem to add anything here.  The following is
equivalent.  (Or am I missing something?)

	> $FILE


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