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”

Watch Free Movies

$ 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

Scroll to Top