Sed in place on privileged symlink

J V

Once upon a time I used sed and tee combined to alter a privileged file in place. Because the shell itself doesn't have root privileges I had to get creative to write command output into files:

sed -r 'script' "/etc/hosts" | sudo tee "/etc/hosts" 1>/dev/null

At the time, this worked wonderfully. It was only after I started using my hosts file for ad blocking that I realised the pipe is limited to a buffer, after which tee will gladly overwrite the file and the rest of the file will disappear.

I also considered a much simpler version:

sed -r 'script' "/etc/hosts" > "/etc/hosts.tmp" && sudo mv "/etc/hosts.tmp" "/etc/hosts"

Unfortunately, this too won't work, since the file in question is a symlink. A mv would overwrite the symlink, as opposed to the file underneath.

My current solution is this:

sed -r 'script' "/etc/hosts" | sudo tee "/etc/hosts.tmp" 1>/dev/null &&
    sudo cat "/etc/hosts.tmp" | sudo tee "/etc/hosts" 1>/dev/null &&
    sudo rm "/etc/hosts.tmp"

Ew! How verbose!

Is there a cleaner way of doing this?

Aaron

If you're using GNU sed, you could use its -i in-place edition flag, so that the results of its execution is directly saved to the file it process. Then you just have to sudo the sed command and you should be fine.

Additionnaly to write the file rather than the link, use sed's --follow-symlinks flag.

In other cases, check this answer to a similar question on SO.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

sed edit file in place

From Dev

How to use sed to change lines in place on a mac (Darwin)

From Dev

In-place sed command not working

From Dev

Need help editing files in place with sed

From Dev

Sed interpreter script - In place

From Dev

sed/awk copy paste snippet from one place to other place in same file

From Dev

in-place backreferencing with sed

From Dev

Place all sed commands into one shell script file

From Dev

How can I achieve portability with sed -i (in-place editing)?

From Dev

How to report "sed" in-place changes

From Dev

Place all sed commands into one shell script file

From Dev

How can I use a back-reference in place of a filename in sed?

From Dev

Sed --- replace a character in a matched line in place?

From Dev

How to use sed to change lines in place on a mac (Darwin)

From Dev

Need help editing files in place with sed

From Dev

sed with multiple expression for in-place argument

From Dev

sed, find and place after and before

From Dev

Efficient in-place header removing for large files using sed?

From Dev

BASH: replacing PERL with SED for in-place substitution

From Dev

Apply the "expand" command to a file in-place like sed -i

From Dev

Does sort support sorting a file in-place, like `sed --in-place`?

From Dev

Different behavior from sed when executed in place

From Dev

sed extract first field and move to specific place

From Dev

How to exchange two patterns in place with `sed`?

From Dev

sed to remove url in-place from file

From Dev

How to change a file in-place using awk? (as with "sed -i")

From Dev

Insert variable in specific place using sed -i

From Dev

Find and sed: how to replace files in place?

From Dev

Unexpected in-place sed behavior

Related Related

HotTag

Archive