Artificial intelligent assistant

How do I prevent sed -i from destroying symlinks? Why does `sed -i` executed on symlink destroys that link and replaces it with destination file? How to avoid this? eg. $ ls -l pet* -rw-rw-r-- 1 madneon madneon 4 mar 23 16:46 pet lrwxrwxrwx 1 madneon madneon 6 mar 23 16:48 pet_link -> pet $ sed -i 's/cat/dog/' pet_link $ ls -l pet* -rw-rw-r-- 1 madneon madneon 4 mar 23 16:48 pet -rw-rw-r-- 1 madneon madneon 4 mar 23 16:49 pet_link And why isn't it considered a bug?

The `-i`/`--in-place` flag edits a file in place. By default, `sed` reads the given file, processes it outputting into a temporary file, then copies the temporary file over the original, without checking whether the original was a symlink.

GNU `sed` has a `--follow-symlinks` flag, which makes it behave as you want:


$ echo "cat" > pet
$ ln --symbolic pet pet_link
$ sed --in-place --follow-symlinks 's/cat/dog/' pet_link
$ cat pet
dog

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 8e4cb6e02d459e81f31a27635c737699