When you attempt to modify a file without having write permissions on it, you get an error:> touch /tmp/foo && sudo chown root /tmp/foo> echo test > /tmp/foozsh: permission denied: /tmp/fooSudoing doesn't help, because it runs the command as root, but the shell handles redirecting stdout and opens the file as you anyway:> sudo echo test > /tmp/foozsh: permission denied: /tmp/fooIs there an easy way to redirect stdout to a file you don't have permission to write to, besides opening a shell as root and manipulating the file that way?> sudo su# echo test > /tmp/foo
Redirecting stdout to a file you don't have write permission on
shell;permissions;io redirection;sudo;io
Yes, using tee. So echo test > /tmp/foo becomes echo test | sudo tee /tmp/fooYou can also append (>>)echo test | sudo tee -a /tmp/foo