How do I use sudo to redirect output to a location I don't have permission to write to?

前端 未结 15 1603
情深已故
情深已故 2020-11-22 07:23

I\'ve been given sudo access on one of our development RedHat linux boxes, and I seem to find myself quite often needing to redirect output to a location I don\'t normally h

15条回答
  •  迷失自我
    2020-11-22 07:56

    Don't mean to beat a dead horse, but there are too many answers here that use tee, which means you have to redirect stdout to /dev/null unless you want to see a copy on the screen.

    A simpler solution is to just use cat like this:

    sudo ls -hal /root/ | sudo bash -c "cat > /root/test.out"
    

    Notice how the redirection is put inside quotes so that it is evaluated by a shell started by sudo instead of the one running it.

提交回复
热议问题