Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

1  
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
add comment

2 Answers 2

up vote 15 down vote accepted

Use the command below

echo "New line to write" | sudo tee -a file.txt
share|improve this answer
1  
That worked like a charm, thank you. –  Toby Jul 11 '12 at 11:27
1  
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
add comment

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
add comment

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