Super User is a question and answer site for computer enthusiasts and power users. 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:
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.

up vote 196 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
On OS X tee only seems to have an -a flag. – user495470 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 .