Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Redirecting stdout to a file you don't have write permission on

I am trying to append a line of text to a write protected file.

I tried to accomplish this with sudo echo "New line to write" >> file.txt but I get a permission denied error — presumably because it is trying to sudo the string, not the act of appending it to a file.

If I run sudo vi file.txt and authenticate I can happily write away.

Any help would be greatly appreciated.

share|improve this question

marked as duplicate by Michael Mrozek Jul 11 '12 at 19:49

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.

Did you mean sudo echo "New line .. in your post? – Levon Jul 11 '12 at 11:28
I did indeed @Levon, thanks - fixed. – Toby Jul 11 '12 at 11:30
up vote 16 down vote accepted

Use the command below

echo "New line to write" | sudo tee -a file.txt
share|improve this answer
That worked like a charm, thank you. – Toby Jul 11 '12 at 11:27
If you're concatenating a lot of data, and/or you're on a particularly slow (or expensive) connection, you should add >/dev/null to the tee side of the pipe. – Alexios Jul 11 '12 at 14:38

I always do such stuff like this: su -c "echo \"Appended.\" >> test.txt" (and I'd be happy to learn how (if) it differs from those other solutions).

share|improve this answer

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