Writing data to a file

> echo "hello" > myfile.txt
> Permission denied

Simple enough, sudo to the rescue!

> sudo echo "hello" > myfile.txt
> Permission denied

The sudo applies to the echo command, instead of the “>” operator

The fix? Use tee - tee reads from STDIN and writes to the file and to STDOUT - but more importantly for our case, it lets us put the sudo where we need it:

> echo "hello" | sudo tee myfile.txt
hello
> cat myfile.txt
hello
  • Categories
comments powered by Disqus