Skip to main content
6 of 6
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/

Redirecting output in the shell, permissions

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 User1

If I do this:

php script.php

I get a permission denied error. Fair enough; I'm User2, not User1. If I do

sudo php script.php

this 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 debug

It 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.txt

This works

sudo touch results.txt
sudo chown User1 results.txt
sudo chmod 777 results.txt
sudo php script.php debug >> results.txt

Alternatively, this works

sudo su -
php script.php debug >> results.txt

So 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.txt

The 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?