Bug #524

sed -i usage: link("test", "test-e") Err#17 EEXIST

Added by Piotr Jasiukajtis over 7 years ago. Updated over 7 years ago.

Status:ResolvedStart date:2010-12-16
Priority:NormalDue date:
Assignee:Garrett D'Amore% Done:


Category:cmd - userland programs
Target version:-
Difficulty: Tags:


Illumos-gate revision 13258, debug build.

$ echo "test" > test
$ sed -i -e 's/test/test123/' test 
$ sed -i -e 's/test/test123/' test 
sed: link(): File exists

$ truss -t link sed -i -e 's/test/test123/' test 
link("test", "test-e")                          Err#17 EEXIST
sed: link(): File exists

$ sed -i "" -e 's/test/test123/' test 

It seems on FreeBSD sed replaces a backup file.


#1 Updated by Garrett D'Amore over 7 years ago

  • Assignee set to Garrett D'Amore

This is a regression I introduced. (The FreeBSD code used rename, instead of link. I changed that because the rename method left the possibility of exiting without leaving even the original file intact.)

Fortunately, the fix is super easy.

diff -r feaf4b4bed62 usr/src/cmd/sed/main.c
--- a/usr/src/cmd/sed/main.c    Wed Dec 15 22:28:29 2010 -0800
+++ b/usr/src/cmd/sed/main.c    Thu Dec 16 08:09:33 2010 -0800
@@ -336,6 +336,8 @@
         if (infile != NULL) {
             (void) fclose(infile);
             if (*oldfname != '\0') {
+                /* if there was an backup file, remove it */
+                (void) unlink(oldfname);
                 if (link(fname, oldfname) != 0) {
                     (void) unlink(tmpfname);

#2 Updated by Garrett D'Amore over 7 years ago

Btw, -i requires an argument. Apparently GNU sed doesn't. Not sure how that is achieved, but this might be a GNUism that we don't quite achieve.

#3 Updated by Garrett D'Amore over 7 years ago

  • Status changed from New to Resolved
  • % Done changed from 0 to 100

Pushed with:

changeset: 13259:8dc1138162bb
tag: tip
user: Garrett D'Amore <>
date: Thu Dec 16 09:43:44 2010 -0800
524 sed -i usage: link("test", "test-e") Err#17 EEXIST
Reviewed by:
Reviewed by:
Reviewed by:
Approved by:

Also available in: Atom