Cannot echo "hello" > x.txt even with sudo?

Possible Duplicate:
sudo & redirect output

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

[email protected]:/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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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 (>>)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply