When you write sudo somecommand > ~/errors.txt
, the shell that's calling sudo
(and is running as you) is the one performing the redirection and opening ~/errors.txt
. See Redirecting stdout to a file you don't have write permission on. Usually the problem in that case is that you want root to write to the file; see the linked question for ways to do this.
Here, it's odd that you cannot write to a file in your home directory. Chances are that you previously saved some output as root in /home/user/errors.txt
, and that file now exists and belongs to root. Remove the file (you can do that as long as you have write permission on /home/user
, and then you'll be able to create it as your user.
rm ~/errors.txt
sudo tail /var/log/apache2/error.log > ~/errors.txt
If the file truly doesn't exist, then you don't have write permission in your home directory. While technically possible, and actually occasionally useful for some restricted users, it's very unusual.
echo hi > ~/errors.txt
? Is/home/user
your correct home directory (or did bash somehow get confused about where your home directory is)? – cjm Mar 20 '12 at 06:32user
the user who runs the command? – user unknown Mar 20 '12 at 06:56sudo
.sudo
does not allow redirection. too many ways for people to be able to use that to do naughty things not encompassed in thesudoers.conf
file. As an alternative, you could runsudo bash -c "tail /var/log/apache2/error.log > ~/errors.txt"
to scrape the end of errors.log to the file in your home dir. – Tim Kennedy Mar 20 '12 at 18:47