Re: to make file size o in a shell ?

From: Floyd L. Davidson (floyd_at_barrow.com)
Date: 03/30/04


Date: Mon, 29 Mar 2004 18:46:19 -0900


"David Schwartz" <davids@webmaster.com> wrote:
>"Floyd L. Davidson" <floyd@barrow.com> wrote:
>> "somez72" <somez72@chollian.net> wrote:
>>>
>>>I just want to truncate the file(tmp/mylog.dat) with size 0 , in a shell
>>>code.
>>
>> >tmp/mylog.dat
>
> That won't work. His next write will restore the file to its original
>size.

That is precisely the way it is done (typically for syslog files
in /var/log, as an example), assuming the intention is to avoid
having to re-open the log file after it has been
truncated/deleted.

> He has to open the file in append mode. Otherwise, he's going to have to
>code exactly what he wants because the system has no idea what it should do.
>
> For example, suppose the file is 1,024 bytes long. He seeks to byte 43
>and is about to write 10 bytes when the file is truncated to size zero.
>Where should the forthcoming 10 byte write go?

Yes, his C code has to comply. The point is, the file need not
be closed and reopened, which is exactly what must happen if it
is rm'd.

--
Floyd L. Davidson           <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@barrow.com


Relevant Pages