Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Possible Duplicate:
sudo & redirect output

I'm trying to create a file in /var/www, but even with sudo this fails:

user@debVirtual:/var/www$ sudo echo "hello" > f.txt
-bash: f.txt: Permission denied

When I use sudo nano, I can save something to this file.

Why can't I use sudo echo?

share|improve this question

marked as duplicate by Lekensteyn, Takkat, jrg Feb 15 '12 at 22:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 16 down vote accepted

The redirection is done by the shell before sudo is even started. So either make sure the redirection happens in a shell with the right permissions

sudo bash -c 'echo "hello" > f.txt'

or use tee

echo "hello" | sudo tee f.txt  # add -a for append (>>)
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.