Discussion:
'sed -i' fails if the file has sufficient perms but not its directory
(too old to reply)
Toralf Förster
2015-07-03 18:22:33 UTC
Permalink
I tried this, which won't work :

***@tor-relay ~ $ sed -i -e 's/j1/j2/g' perl-5.22/etc/portage/make.conf
sed: couldn't open temporary file perl-5.22/etc/portage/sedw9SFZf: Permission denied

whereas I'm allowed to edit that file as the user tinderbox

***@tor-relay ~ $ vi perl-5.22/etc/portage/make.conf

/me wonders if sed relies on having a temp copy of that file in the same dir or not.
--
Toralf, pgp key: 872AE508 0076E94E
Jim Meyering
2015-07-03 22:02:48 UTC
Permalink
Post by Toralf Förster
sed: couldn't open temporary file perl-5.22/etc/portage/sedw9SFZf: Permission denied
whereas I'm allowed to edit that file as the user tinderbox
/me wonders if sed relies on having a temp copy of that file in the same dir or not.
Yes, sed -i does require write access to each directory
containing a file that it modifies. That is necessary so that
the temporary file it creates can be renamed atomically back
onto the original, assuming all processing is successful.
Bob Proulx
2015-07-04 01:51:06 UTC
Permalink
Post by Toralf Förster
sed: couldn't open temporary file perl-5.22/etc/portage/sedw9SFZf: Permission denied
Correct. If you want to use -i you will need write permission to the
directory. This is the same as other tools such as perl -i and awk -i too.
Post by Toralf Förster
whereas I'm allowed to edit that file as the user tinderbox
The behavior depends upon the vi implementation. Some read it into
memory, some into spill files in /tmp, some into .swp files in the
same directory.
Post by Toralf Förster
/me wonders if sed relies on having a temp copy of that file in the same dir or not.
The documentation for sed says this:

'-i[SUFFIX]'
'--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
'sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
...
When the end of the file is reached, the temporary file is renamed
to the output file's original name. The extension, if supplied, is
used to modify the name of the old file before renaming the
temporary file, thereby making a backup copy(2)).

Bob

Loading...