0

I need to write to a.txt. The file is owned by root with a read-write access. But still I cannot write over it with a sudo. Why?

% ls -l
total 8
-rw-r--r--  1 root  staff  6 Mar 24 00:30 a.txt

% sudo echo "hi" >> a.txt
zsh: permission denied: a.txt
zell
  • 9,830
  • 10
  • 62
  • 115
  • Does this answer your question? [How do I use sudo to redirect output to a location I don't have permission to write to?](https://stackoverflow.com/questions/82256/how-do-i-use-sudo-to-redirect-output-to-a-location-i-dont-have-permission-to-wr) – ilkkachu Mar 24 '21 at 14:55

1 Answers1

1

The redirection happens before the commands are run, i.e. using the original user.

Work-around:

sudo sh -c 'echo "hi" >> a.txt'
choroba
  • 231,213
  • 25
  • 204
  • 289