5

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 –  Mar 09 '18 at 14:12

2 Answers2

10

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?

fedorqui
  • 7,861
  • 7
  • 36
  • 74
0

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.