Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Possible Duplicate:
sudo unable to write to /etc/profile

I want to do:

echo "something" >> /etc/config_file

But, since only the root user has write permission to this file, I can't do that. But the following also doesn't work.

sudo echo "something" >> /etc/config_file

Is there a way to append to a file in that situation without having to first open it with a sudo'd editor and then appending the new content by hand?

share|improve this question

marked as duplicate by random May 1 '10 at 9:06

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.

2 Answers 2

up vote 96 down vote accepted

The right tool for the right task: 'tee'

tee - read from standard input and write to standard output and files

So your commandline becomes

% echo "output" | sudo tee -a file

The advantage of tee over executing Bash with administrative permissions is:

  • You do not execute Bash with administrative permissions
  • Only the 'write to file' part runs with advanced permissions
  • Quoting of a complex command is much easier
share|improve this answer
1  
On OS X tee only seems to have an -a flag. –  ؘؘؘؘ Jun 20 '12 at 14:48

The redirection is executed in the current shell. In order to do the redirection with elevated privileges, you must run the shell itself with elevated privileges:

sudo bash -c "somecommand >> somefile"
share|improve this answer

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