sed -i destroys symlink
Bug #367211 reported by
Miek Gieben
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
sed (Ubuntu) |
Fix Released
|
Undecided
|
Unassigned |
Bug Description
When using 'sed -i' on a symlink the symlink gets destroyed.
Observe:
% ls -l myfile symlink
-rw-rw-r-- 1 miekg miekg 12 Apr 26 10:53 myfile
lrwxrwxrwx 1 miekg miekg 6 Apr 26 10:53 symlink -> myfile
# do an edit
% sed -i 's/hello//' symlink
% ls -l myfile symlink
-rw-rw-r-- 1 miekg miekg 12 Apr 26 10:53 myfile
-rw-rw-r-- 1 miekg miekg 7 Apr 26 10:54 symlink
And gone is your symlink.
This either a feature or a bug :)
To post a comment you must log in.
This is due to using rename(), in the file lib/utils.c (is sed's source) at line 324 in the function:
ck_rename (from, to, unlink_if_fail):
int rd = rename (from, to);
from is now a tmp-file and to is the original file. According to the manpage of rename(2):
If oldpath refers to a symbolic link the link is renamed; if newpath refers to a symbolic link
the link will be overwritten.
So the later thing happens.