Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know we can use below format to redirect the screen output to a file:

$ your_program > /tmp/output.txt

However when I used below command, it says "-bash: /home/user/errors.txt: Permission denied"

sudo tail /var/log/apache2/error.log > ~/errors.txt

May I know how to make this output works? The ~/errors.txt doesn't exist. Do I need to create this txt file first before I use the redirect command?

share|improve this question
1  
What happens if you type 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 6:32
    
Is user the user who runs the command? –  user unknown Mar 20 '12 at 6:56
    
yes, the user is the user who runs the command –  Xianlin Mar 20 '12 at 8:03
    
This is normal behavior for sudo. sudo does not allow redirection. too many ways for people to be able to use that to do naughty things not encompassed in the sudoers.conf file. As an alternative, you could run sudo 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
add comment

2 Answers

up vote 12 down vote accepted

Behind the pipe, the sudo doesn't work. I don't know why you can't write to your home - maybe the file belongs to root?

 sudo tail /var/log/apache2/error.log | sudo tee ~/errors.txt

Maybe you need a different user behind the pipe. For sure, you don't need a preexisting file.

share|improve this answer
    
Yes, that worked... –  Xianlin Mar 20 '12 at 8:02
1  
@Xianlin if it worked, would you accept the answer? –  Legolas Mar 20 '12 at 8:19
    
As to the permission problem, maybe $HOME is NFS-mounted with rootsquash? –  Ansgar Esztermann Mar 20 '12 at 10:03
    
the $HOME is not NFS mounted. –  Xianlin Mar 20 '12 at 15:19
add comment

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.

share|improve this answer
    
I tried delete '/home/user/errors.txt' and use your above command. It didn't work, still forbid me to write to home directory. Anyway I guess the link you provided can use as my solution. It's the same as the first answer in this post. However you have given some detailed explanation on the redirecting stdout to a file and I thank you for that. –  Xianlin Mar 21 '12 at 1:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.