0

Permissions:

ls -al file
-rwxrwxr-x  1 root  wheel 

User group:

groups
wheel 

If I do this:

sed -i'' -e '/Marker/i\'$'\n''text string'$'\n' file 

I get an error:

sed: ../file: Permission denied

But at the same time I can read, write and execute this file. As it shown in permissions.

Why sed is not working? I use the same User and the same file. Okay, owner is root, but I have read and write permissions

 uname -a
FreeBSD srv 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016     root@releng2.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC  amd64
  • Owner is root, and if you are not in the group wheel then you don't have write permissions. – Weijun Zhou Jan 15 '18 at 7:34
  • 2
    @WeijunZhou, they just said they're members of wheel and that they can read and write the file... – ilkkachu Jan 15 '18 at 7:38
  • At least that's not what I can see from the last line of the question. – Weijun Zhou Jan 15 '18 at 7:39
2
sed -i'' -e ... file 

The BSD sed requires an argument to -i, as opposed to GNU sed that accepts an optional argument to it.

FreeBSD sed man page:

-i    extension     

Edit files in-place similarly to -I

vs. GNU sed man page:

  -i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if SUFFIX supplied)

Note the brackets or lack of them.

Also, in shell -i'' is the same as just -i, the quotes surrounding an empty string are just removed.

So, what sed thinks you're asking is to create a backup file with the extension -e, and unless you have write permission to the directory, too, you won't be able to do that.


Even without an argument or with an empty argument to -i, sed will probably create a temporary file where it writes the resulting output before moving it to the original name. At least GNU sed creates the temporary file in the same directory as the original, so again you need to have write permission to that directory.

$ strace -etrace=open,rename sed -i -e '1d' foo
[...]
open("foo", O_RDONLY)                   = 3
open("./sedD9J9tV", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
rename("./sedD9J9tV", "foo")            = 0

To work around that, you could create a temporary file elsewhere, and then move its contents to place:

$ sed -e '...' file > /tmp/file.tmp
$ cat /tmp/file.tmp > file && rm /tmp/file.tmp
  • I don't think so. I have used sed -i'' without problem. – Weijun Zhou Jan 15 '18 at 7:38
  • I sure about this sed, because I made other test file and I successfully use that sed script. – rGA145 Jan 15 '18 at 7:38
  • But you gave me idea about directory permissions... file is located in directory with drwxr-xr-x (so group have no write permissions). I don't clearly understend how this sed works, but if it created file-clone, it can be reason why I can't use It. I will try changing perm-s to directory. – rGA145 Jan 15 '18 at 7:40
  • @HelloKatty, try something like mkdir z; cd z; echo x > foo; sed -i'' -e "1d" foo; ls -l, that should tell you how your sed interprets that. – ilkkachu Jan 15 '18 at 7:46
  • @ilkkachu The error message should be file-e: Permission denied in this case. But eventually you are right: Without suffix a temporary file gets created and can't in this case; just the error message is confusing. – Philippos Jan 15 '18 at 8:07
0

So the problem was with parent path, where script is located. I think (I really dont know how sed works), sed tried to create a clon-file to add a text string and can't because of 755 path permissions. 775 solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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