[How To] Redirect Text to a File Using sudo
|In Ubuntu Linux (and a number of other systems) it is customary to use sudo to obtain root privileges, and often there is the task of writing (for example, via echo) any value to a file owned by the root user.
Many do it intuitively and incorrectly:
sudo echo Value > /path
This example does not work for the reason that using sudo you get root privileges, but the redirection to bsh (“>”) already works from your regular account.
Correctly, you can do this:
echo Value | sudo tee /path
Here is example that changes the processor’s operation scheme to “conservative” and sets starting frequency to 1.15 GHz:
echo conservative | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor echo 1150000 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
Test Environment
Ubuntu 19.10 (Eoan Ermine)
VMware Workstation 15.1.0