Skip to main content
add sudo tag
Link
xenoterracide
  • 61.4k
  • 79
  • 196
  • 252
Source Link
Michael Mrozek
  • 95.7k
  • 40
  • 245
  • 236

Redirecting stdout to a file you don't have write permission on

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/foo
zsh: permission denied: /tmp/foo

Sudoing 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/foo
zsh: permission denied: /tmp/foo

Is 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