When sudo echo ‘test’ > /root/test gives permission denied
September 3rd, 2008 by Paul Maunders
If you get a permission denied error when trying to redirect the output of a sudo command then the reason for this is normally because the superuser permissions only apply to the first part of the statement, e.g. the echo command. They do not carry through to the bash redirection.
paul@backups:~$ sudo echo 'test' >> /root/test -bash: /root/test: Permission denied
One way to get round this is to use the tee command, as follows:
echo 'test' | sudo tee -a /root/test
Note that the -a switch means append to the file if it already exists.
Posted in linux
October 13th, 2008 at 6:11 am
Or you could just use “sudo -s” and then type:
echo “test” >> /root/test
November 12th, 2008 at 4:19 pm
This is great. Now I can control my fans through shell scripts, which wouldn’t work with Pablo’s suggestion.
sudo echo 3 | sudo tee /proc/acpi/fan/FAN1/state