sed permission denied when overwriting file

前端 未结 9 549
我在风中等你
我在风中等你 2021-01-11 12:07

I am trying to use sed to overwrite my index.php file, but I am getting an error:

$ sed -i \'s@@

        
相关标签:
9条回答
  • 2021-01-11 12:45

    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'

    • The <command> <<< '<my_input_string>' construct will pipe '<my_input_string>' into the <command> as standard input
    • The $'...' construct will be parsed for special characters. In this case the \n splits the string into two lines
    • You can use any construct you want to generate and pipe in the input (echo, printf, etc.)

    Standard 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

    0 讨论(0)
  • 2021-01-11 12:52

    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!

    0 讨论(0)
  • 2021-01-11 12:52

    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 +++
    
    0 讨论(0)
  • 2021-01-11 12:52

    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

    0 讨论(0)
  • 2021-01-11 12:53

    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.

    0 讨论(0)
  • 2021-01-11 12: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.

    0 讨论(0)
提交回复
热议问题