Redirecting the output from a command to a file is pretty trivial - you accomplish it with something like this:

echo "testing 1234" > dest_file.txt

However if you naively try to throw sudo in there in order to write to a file that you don’t have write access to, you’d be sorely disappointed, like I have been. Thus, trying the following fails:

sudo echo "testing 1234" > dest_file.txt

This would run the echo command as root, but the redirect part still runs as your underpowered user. Additionally, putting sudo after the redirection operator thusly:

echo "testing 1234" > sudo dest_file.txt

Would create a file called sudo :D Not what we want!

Solution!

One common way to accomplish the above is to use the tee command in this way:

echo "testing 1234" | sudo tee dest_file.txt

Things to remember is that you’d have to “pipe” (|) the output instead of redirecting (>) as tee is a command and if you want to append to the destination file you need to specify the -a flag. Like this:

echo "testing 1234" | sudo tee -a dest_file.txt

Boom! Magic :P