2

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

I'm trying to create an md5sum for an ISO image created with the Ubuntu Customization Kit tools. The ISO is created by the tools, which have to be run with sudo, in

~/tmp/remaster-new-files/ 

which has permissions:

drwxr-xr-x 2 root root remaster-new-files

So I cd to that directory and run

sudo md5sum my.iso > my.iso.md5

and I get the following error:

bash: my.iso.md5: Permission denied

I can create the md5 sum somewhere else and use sudo mv to move it into place, exactly where it would be if the sudo md5sum command succeeded. Also, if I change user to root with sudo su root, I can run the md5sum command successfully. Why can't I use sudo to create files in this directory, given that I can use sudo to move files to it?

1 Answers1

11

The problem is that the redirection is done from the shell before running the command, as the current user, so sudo do not come into play.

Use instead

md5sum my.iso | sudo tee my.iso.md5
enzotib
  • 51,661
  • Thanks - that's exactly the nugget of information I was missing! – Tim Beattie Dec 21 '11 at 11:05
  • That's an interesting way of doing it. Most of the time suggestions just wrap the command and redirection in a sudo bash -c "" command... +1 – Izkata Dec 21 '11 at 15:40
  • For those who haven't used tee, note that this will also print the contents to the console, and so shouldn't be used on binary or untrusted inputs. – Shelvacu May 05 '16 at 20:39