Home > Uncategorized > write to file in linux

write to file in linux

If you want to write to a file in linux you can do the following:

echo "something" > /var/log/test.log

but if the test.log file is only writable by root, then you have to use sudo. 

sudo echo "semothing" > /var/log/test.log

but this also fails because it is only echo that is running as sudo not the actual writing to file.

The way I do this is my using the tee command:

echo 'something' | sudo tee /var/log/test.log

You could also do:

echo "echo 'something' > /var/log/test.log" | sudo sh

but that seems more hacky….

Categories: Uncategorized Tags:
  1. me
    September 29th, 2011 at 13:25 | #1

    Cool

  1. No trackbacks yet.