Bug 532006 - sed -i of symbolic links create a file
Summary: sed -i of symbolic links create a file
Alias: None
Product: Fedora
Classification: Fedora
Component: sed
Version: 11
Hardware: All
OS: Linux
Target Milestone: ---
Assignee: Jiri Moskovcak
QA Contact: Fedora Extras Quality Assurance
Depends On:
TreeView+ depends on / blocked
Reported: 2009-10-30 09:12 UTC by Michele Marcionelli
Modified: 2015-02-01 22:49 UTC (History)
4 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Last Closed: 2009-10-30 11:19:10 UTC
Type: ---

Attachments (Terms of Use)

Description Michele Marcionelli 2009-10-30 09:12:01 UTC
Description of problem:
when I apply "sed -i" on a symbolic link of a file (for instance /etc/grub.conf), sed will remove the symbolic link and create a file.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
root# echo "abc" > x1
root# ln -s x1 x2
root# sed -i 's/abc/cba/' x2
Actual results:

root# ls -l x?
-rw-r--r--. 1 root root 4 2009-10-30 10:06 x1
-rw-r--r--. 1 root root 4 2009-10-30 10:07 x2

root# cat x?

Expected results:

root# ls -l x?
-rw-r--r--. 1 root root 4 2009-10-30 10:06 x1
lrwxrwxrwx. 1 root root 2 2009-10-30 10:06 x2 -> x1

root# cat x?

Additional info:

Comment 1 Paolo Bonzini 2009-10-30 11:19:00 UTC
This is mentioned in i/usr/share/doc/sed-4.2.1/BUGS:

-i clobbers read-only files

  In short, `sed d -i' will let one delete the contents of
  a read-only file, and in general the `-i' option will let
  one clobber protected files.  This is not a bug, but rather a
  consequence of how the Unix filesystem works.

  The permissions on a file say what can happen to the data
  in that file, while the permissions on a directory say what can
  happen to the list of files in that directory.  `sed -i'
  will not ever open for writing  a file that is already on disk,
  rather, it will work on a temporary file that is finally renamed
  to the original name: if you rename or delete files, you're actually
  modifying the contents of the directory, so the operation depends on
  the permissions of the directory, not of the file).  For this same
  reason, sed will not let one use `-i' on a writeable file in a
  read-only directory, and will break hard or symbolic links when
  `-i' is used on such a file.

You can use --follow-symlinks to get the desired behavior.

Comment 2 Michele Marcionelli 2009-10-30 11:44:34 UTC
Ciao Paolo

but this is a new feature, isn't it? For instance I have not this problem with RHEL 5.4:

root# rpm -qa sed

On RHEL 5.4 I have another problem: sed on a symbolic link works only with the full path:

root# sed -i 's/abc/cba/' x2
sed: ck_follow_symlink: couldn't lstat x/x1: No such file or directory

root# sed -i 's/abc/cba/' /tmp/sed/x2

and this without the "--follow-symlinks" option (which doesn't exist for this version):

root# ls -l x?
-rw-r--r-- 1 root root 4 Oct 30 12:31 x1
lrwxrwxrwx 1 root root 2 Oct 30 12:31 x2 -> x1


Comment 3 Paolo Bonzini 2009-10-30 12:14:28 UTC
The RHEL bug is bug 490473.  I added you to the CC list there.

The Fedora behavior is "a new feature" indeed, or you could say it was a bug before in that it did not follow what the manual said.  It's a bit unfortunate either way, I agree.

Note You need to log in before you can comment on or make changes to this bug.