10

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?

4
  • 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, 2012 at 6:32
  • Is user the user who runs the command? Mar 20, 2012 at 6:56
  • yes, the user is the user who runs the command
    – Xianlin
    Mar 20, 2012 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. Mar 20, 2012 at 18:47

2 Answers 2

19

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.

3
  • Yes, that worked...
    – Xianlin
    Mar 20, 2012 at 8:02
  • As to the permission problem, maybe $HOME is NFS-mounted with rootsquash? Mar 20, 2012 at 10:03
  • the $HOME is not NFS mounted.
    – Xianlin
    Mar 20, 2012 at 15:19
7

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.

1
  • 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, 2012 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .