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 this:

sudo echo "something" >> /etc/config_file

also doesn't work. Is there any 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?

link|improve this question

73% accept rate
feedback

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

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

2 Answers

up vote 13 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 --append 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
link|improve this answer
feedback

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"
link|improve this answer
feedback

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