help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Prepending text to the beginning of a file


From: Greg Wooledge
Subject: Re: Prepending text to the beginning of a file
Date: Thu, 16 Jun 2022 17:19:15 -0400

On Thu, Jun 16, 2022 at 05:12:27PM -0400, Robert E. Griffith wrote:
> I think that the best you can do is either use a temporary file or, if you
> know the file is small enough, read it entirely into memory and then write
> the new data and then the old back out to the same file.

The latter is inherently dangerous, because the writing process, or the
system it's running on, could be terminated during the write.  (Crash,
power loss, fatal software bug, etc.)

In addition to that, a non-atomic write operation would leave the file
in an indeterminate state.  Any other process that reads this file while
the write is underway will get corrupt data.

Writing the new data to a new file is the only safe choice.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]