sudo tee

When we don’t want to run a shell with sudo but still want to redirect output to a location we don’t have permission to access we can use the sudo tee trick.

$ sudo cat /root/file | sudo tee /whatever/test.out > /dev/null

The redirect to /dev/null is there to stop tee from outputting to the screen.

We have to use sudo with tee because a normal redirection performed by the shell is running with our users permissions.

This could be further simplified with:

$ sudo cat /root/file | sudo dd of=/whatever/test.out

dd is great to write large files (usually images to disks), but also works equally well with small ones.