7

I have tried:

sudo "some string" >> test.txt

But I am getting permission denied warning.

Can anyone help?

kenorb
  • 6,499
  • 2
  • 46
  • 54
ChrisInCambo
  • 1,751
  • 3
  • 15
  • 13

5 Answers5

14

Depends on how much access your administrator has given you via sudo. The simplest answer, assuming that you have permission to do so, is to run "sudo -s" to get a privileged shell and then just do your

echo somestring >> test.txt

as 'normal'. If you need to do it automatedly:

sudo /bin/sh -c 'echo somestring >> test.txt'

The reason that what you have won't work (other than the fact that you left what I assume to be an "echo" command out) is that the file redirection happens in the context of your shell and the sudo only applies to the command you told it to run.

wfaulk
  • 6,878
  • 7
  • 46
  • 75
13
sudo bash -c "echo 'some string' >> test.txt"

The problem in your original try is that the ">>" is happening in the shell running as you; bash opens the file, not whatever is being run by sudo. In my version, you're passing the whole command including the redirection to a bash run by sudo.

freiheit
  • 14,544
  • 1
  • 47
  • 69
  • have you considered sudo -s instead of firing up /bin/sh explicitly? – Jeremy L Sep 24 '09 at 21:35
  • Nerdling: On my systems, "sudo -s" runs the shell you specify, it doesn't really support running complicated multi-argument things like this. – freiheit Sep 24 '09 at 21:47
13
% echo "string" | sudo tee -a test.txt
akira
  • 531
  • 2
  • 11
  • 1
    Has the side effect of echoing the string to stdout, but nice and short... – freiheit Sep 24 '09 at 21:50
  • 3
    if you don't want to echo to stdout echo "string" | sudo tee test.txt > /dev/null – Aaron Brown Sep 24 '09 at 22:08
  • 3
    the real value of 'tee' is that all the 'work' is done in the context of the normal user and only the output is written with 'sudo' powers. for me thats more important than the shortness and more 'correct' than the other answers, allthough they work of coz. – akira Sep 24 '09 at 22:14
  • It's a good solution. – wfaulk Sep 24 '09 at 22:32
  • 1
    +1 This is the best solution because the main part of the command executes with limited privilege, and only the output gets written as root. – lukecyca Sep 25 '09 at 00:01
  • Need to use the -a switch if you don't want to overwrite – Kevin M Aug 18 '10 at 15:37
0

You can try ex-way:

sudo ex +'$put =\"some string\"' -cwq foo.txt

It's simple in-place file editing and it's useful in scripts, so you don't have to do any shell piping.

kenorb
  • 6,499
  • 2
  • 46
  • 54
  • Isn't `sed` easier here? – chicks May 25 '15 at 12:09
  • @chicks `sed` is more stream oriented (can only read the file forward), so you'll have to read the whole file. `ex` is editor (predecessor of `vi`), so you can place the cursor at the end and append the text, so it allows more complex things to do. You still can use `-i` (in-place) in `sed`, but this option is not quite compatible between Linux/Unix and we don't have any pattern to replace here, so I guess it won't work in this case. – kenorb May 25 '15 at 12:24
0

Lots of people recommend tee for this, but I don't like the side-effect of writing to stdout. An alternative is cat:

sh some_script.sh | sudo sh -c 'cat >> somefile.txt'

If cat had an argument like tee's -a flag, that would be a little handier:

sh some_script.sh | sudo pseudocat -a somefile.txt

But unfortunately it doesn't.

Ken Williams
  • 173
  • 1
  • 11