Paul Maunders | Web log
Economics, Business, Telecoms, Tech and Gadgets

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

2 Responses

  1. Pablo Antonio

    Or you could just use “sudo -s” and then type:
    echo “test” >> /root/test

  2. Anthony

    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

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.