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?


1 Answer 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 (>>)
  • what if you already used quote and double quote?
    – tatsu
    Mar 3, 2019 at 18:14
  • 4
    Thanks. Top one didn't work for me on Debian but 2nd option worked perfectly.
    – user283005
    Jul 11, 2019 at 9:32
  • Kind of lengthy + ugly really isn't it.
    – Manachi
    Mar 5, 2021 at 3:15
  • If you need multiple lines as stdin input, use cat <<EOF | sudo tee -a /your/file, and let your input's last line be EOF.
    – WesternGun
    Jul 30, 2021 at 10:56
  • For those who need to do something similar without logging the information to the console (when you're dealing with private information such as secrets for example), you can find more information here: stackoverflow.com/a/18146890/3513260 Apr 17, 2023 at 19:36

Not the answer you're looking for? Browse other questions tagged .