I noticed a strange behaviour when applying an edit using sed -i on a symlink. The documentation sais -i will do an in-place edit. However, the symlink is replaced with a file.

Steps to reproduce:

cd /tmp
echo blah > foo
ln -s foo bar
sed -i -e 's/ah/ub/' bar
ls -l

will result in:

-rw-rw-r--. 1 arogge   arogge      5 Mar  9 15:07 bar
-rw-rw-r--. 1 arogge   arogge      5 Mar  9 15:07 foo

Is this intended behaviour or is it a bug in sed?

  • adding the additional parameter -c makes sed do what I thought it should do anyways – Andreas Rogge Mar 9 '18 at 14:12

This is the expected behaviour.

The -i/--in-place flag edits a temp copy of a file and then moves that copy over the original.

So when you do:

sed -i 'bla' symlink

What sed is doing is:

sed 'bla' symlink > temp_file
mv temp_file symlink

And hence destroying the symlink by placing a regular file in its place.

Info taken from a comment in How do I prevent sed -i from destroying symlinks?

|improve this answer|||||

Does the full listing include something like this

lrw-rw-r--. 1 arogge arogge 5 Mar 9 15:07 bar-e -> foo

It is the intended behaviour. From the man page for sed

-i extension

Edit files in-place, saving backups with the specified extension.

It appears that sed is interpreting the -e as the extension to -i.

|improve this answer|||||
  • 1
    no, that's not what happens. – Andreas Rogge Mar 9 '18 at 14:40

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