If I interrupt sed (stream editor) is there any chance of corrupting the target file?

Example: If I run the command sed -i -- 's/foo/bar/g' file.txt, and kill it before it finishes, is there any chance that the string foo will be replaced by some text other than bar or even corrupting the target file?

What I would expect is that if I kill before the file handler is flushed, then there is a chance of unfinished replacement being in the file. However, I have a feeling that sed is robust for such events, by using some kind of intermediate files before modifying the target. But, I couldn't find details in the documentation.


From the sed manual...


When the end of the file is reached, the temporary file is renamed to the output file's original name.

This option specifies that files are to be edited in-place. GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output.1.
This option implies -s.

When the end of the file is reached, the temporary file is renamed to the output file's original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy2).

This rule is followed: if the extension doesn't contain a *, then it is appended to the end of the current filename as a suffix; if the extension does contain one or more * characters, then each asterisk is replaced with the current filename. This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix, or even to place backup copies of the original files into another directory (provided the directory already exists).

If no extension is supplied, the original file is overwritten without making a backup. 
  • Note that this is not true for perl -p -i -e in which SIGINT will just interrupt perl and leave the file at partial output. – CMCDragonkai Jun 13 '18 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.