Reputation: 3425
I am trying to use sed to overwrite my index.php file, but I am getting an error:
$ sed -i 's@<head>@<head><script type="text/javascript" src="new.js"></script>@' index.php
sed: couldn't open temporary file ./sedmZNx8T: Permission denied
Does anyone know how to fix this?
Upvotes: 22
Views: 95055
Reputation: 1
I had the same problem under Windows 10. I know, not the OPs question but maybe it helps other people comming from Google. The solution for me was to remove/move the folder I am working on from the randsomeware protected folders.
Upvotes: -1
Reputation: 61
I think there is a much easier solution. You can just print the file to the standard output and then use the sed
on the stream. Afterward, you just redirect the stream to the same file. Obviously, it is a risky solution cause if you are not 100% sure that you know what you are doing you might lose the entire content of the file in question.
cat index.php | sed 's@<head>@<head><script type="text/javascript" src="new.js"></script>@' > index.php
Upvotes: 1
Reputation: 833
If the use of sed is not a hard requirement, you can probably use the highly ignored command ed
.
ed -s index.php <<< $'%s@<head>@<head><script type="text/javascript" src="new.js"></script>@\nwq'
<command> <<< '<my_input_string>'
construct will pipe '<my_input_string>' into the <command>
as standard input$'...'
construct will be parsed for special characters. In this case the \n
splits the string into two linesStandard Input String:
%s@<head>@<head><script type="text/javascript" src="new.js"></script>@
wq
The first line is your regex search and replace as command for ed
. The second line tells ed
to write the changes and quit. You may recognize these commands from vi.
source: mrbluecoat.blogspot.com/2014/03/in-place-editing-right-way-hint-dont.html
Upvotes: 1
Reputation: 833
Short answer :
$ chmod +w .
And re-run sed
.
Long answer :
As already said, the problem come from the fact that you don't have write permission on .
(the current directory from which you run sed
). If you are on your own machine/drive then you surely can give yourself the permission to write on this directory, that's what the chmod
do here, unless you want protect it for some reason. However, if your on a network drive and can't change your permission on it then you should ask the one that own the directory to do it or use a workaround solution like copy-modify-past the file somewhere else.
Upvotes: 0
Reputation: 4293
check for the /tmp
folder permission
It should have the following permission
drwxrwxrwt 7 root root 4.0K Nov 16 15:06 tmp
If it is not ok for you then run the following commands
sudo chown root:root /tmp
sudo chmod 1777 /tmp
once this is done, then put a sudo
infront of your sed
command to execute the command as a root user.
This will solve the issue
Upvotes: 1
Reputation: 59
I think you can you "sudo" in front of your command. Like sudo sed -i 's/geteuid/getppid/g' /usr/bin/vlc It worked for me.
Upvotes: 5
Reputation: 1342
You need to have the write permissions to the folder whose file you are changing.
As explained here: https://stackoverflow.com/a/36404569/5381704
Upvotes: 1
Reputation: 311
Really no great answers here. Sorry if thread is dead, it came up in google searches and nothing really answered the question. Although altendky's answer is sort of the same as mine... There IS a workaround...find a world writable directory, say /tmp
cp /etc/file_to_mod /tmp
sed -i -e 's/foo/bar/g' /tmp/file_to_mod
cat /tmp/file_to_mod >/etc_file_to_mod
Hope that helps!
Upvotes: 15
Reputation: 4354
Given that you do not have write access to the directory where index.php
is stored, then generally you would not have access to index.php
...? Assuming this is a 'non-standard' case and you do have write access, sed -i
is trying to create a temp file in the same directory as index.php
(just checked and it is the same directory, not the CWD). Run sed
without -i
and end the line with something like > ~/sed.out
to write the result into a file in your home directory. Then do what you will with that file.
sed 's@<head>@<head><script type="text/javascript" src="new.js"></script>@' index.php > ~/sed.out
Be aware that you while you can cp ~/sed.out index.php
you can not mv ~/sed.out index.php
. It would seem that cp
will put the contents into index.php
(not modifying the directory you do not have write permissions to) while mv
tries to change the directory (that you don't have write permissions to).
Upvotes: 3
Reputation: 185630
Seems like you have a permission issue on the /tmp
dir. (as discussed bellow, you run command in phpshell
, so TMP dir
can be setted elsewhere than /tmp
)
It should looks like :
$ ls -ld /tmp
drwxrwxrwx 333 root root 32768 12 oct. 03:13 /tmp/
explanations
When invoking sed
with -i
flag, sed
create a temporary file in /tmp
dir.
Proof with strace
:
$ strace -f -e trace=file sed -i 's/a/z/' /tmp/a
execve("/bin/sed", ["sed", "-i", "s/a/z/", "/tmp/a"], [/* 94 vars */]) = 0
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
(...)
open("/tmp/sedPSBTPG", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
rename("/tmp/sedPSBTPG", "/tmp/a") = 0
+++ exited with 0 +++
Upvotes: 6