I have a question about permissions. Distro: Debian GNU/Linux 6.0.4 (squeeze)So I have a folder with a php script in it.  Both folder and file are owned by User1If I do this:php script.phpI get a permission denied error.  Fair enough; I'm User2, not User1.  If I do sudo php script.phpthis works because my user account is setup for that.So I wrote this script to accept an optional argument to output some stuff, like a debug mode, so if I do this:sudo php script.php debugIt outputs some stuff to CLI.  So far so good.But sometimes the output from the script is too long and I can't scroll up to see the output from the beginning, so I want to be able to redirect the output to a file. So I have tried the following:This gives me a permission denied error. sudo php script.php debug >> results.txtThis workssudo touch results.txtsudo chown User1 results.txtsudo chmod 777 results.txtsudo php script.php debug >> results.txtAlternatively, this workssudo su -php script.php debug >> results.txtSo what I want to know is... why did the first one give me a permission denied error?  I thought the point of the sudo prefix is that I run the command as root, which works for running the php script...but it seems to me that this doesn't extend to executing the redirect?  Can someone explain why?EDIT:So I found something from another post on here that works:sudo php script.php debug | sudo tee results.txtThe answer there confirms my suspicion about the redirect still being run under my user and not sudo, but it doesn't explain why...can anybody explain why?
Redirecting output in the shell, permissions
debian;permissions;sudo;io redirection
When working on a command line the shell first does the redirections and after that does the execve to the external command. This means that the redirections are performed with unchanged user rights (because the shell doesn't care about sudo getting involved later).The sudo command which could easily do >> results.txt doesn't see that but just gets a changed file descriptor for /dev/stdout.Two possibilities:You make the redirection to a file / directory which is writable to you (User2).You write a wrapper script which does that and call the wrapper script via sudo.