Redirect the standart output to a file which does not have write permissions

When you try to redirect the standard output (stdout) to a file not having write (w) permissions, you often get this error: “Permission denied”

$ echo "test" > a.txt
-bash: a.txt: Permission denied

Do the trick by using sudo tee:

One way to redirect output to a file without write permissions is to use sudo tee:

$ echo test | sudo tee a.txt
test

If you need to append text to a file without write permissions, use tee -a:

$ echo test | sudo tee -a a.txt

The tee option also send output to the terminal. To avoid this, redirect the stdout to /dev/null.

Like this:

$ echo test | sudo tee /tmp/foo > /dev/null

Related reading: Get root access inside vim (by using sudo tee).

Do the trick by using sudo dd:

Another way to reditect output to a file without write permissions is to use sudo dd:

$ echo test | sudo dd of=/tmp/foo

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!