I am trying to use sed to overwrite my index.php file, but I am getting an error:
$ sed -i \'s@@
While editing a file in place, sed creates a temporary file, saves the result and then finally mv the original file with the temporary one.
The problem is that you don't have write permission in the directory where sed is trying to create the temp file.
As the file is /tmp/file, check the permission of the directory where you are running the command.
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).
You need to have the write permissions to the folder whose file you are changing.
As explained here: https://stackoverflow.com/a/36404569/5381704