Sudo doesn't work when redirecting sed output to file

Possible duplicate:
How to use sudo to redirect output to a location to which I do not have write permission?

Let's say I want to change the file "foo" that lives in / home, applying some kind of regular expression to it (via sed ) and putting the result in a file called / home / foo 2.

I do not have read / write access to either / home or foo, so I use sudo . However, I still get permission rejected

sudo sed "s/bar/baz/" <foo >foo2 bash: foo2: Permission denied 

Any ideas? Thanks

+4
source share
2 answers

Use the in place, -i option. Your syntax will look like this:

 sed -i [pattern] filename 
+4
source

Although sh may be a bad idea in general, it will give permission to create a new foo2 file for your shell:

sudo sh -c "sudo sed 's/bar/baz/' <foo >foo2"

+1
source

Source: https://habr.com/ru/post/1443013/


All Articles