Redirecting the output from a command to a file is pretty trivial - you accomplish it with something like this:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
Boom! Magic :P