0

I have a file named foo whose permissions are -rwxr-x--- root and I'm neither the user (root) nor in the group. I have another file named bar, which I want to append to foo. I've tried

sudo cat bar >> foo

but this fails with foo: Permission denied.

dimid
  • 627

2 Answers2

6
cat bar | sudo tee -a foo > /dev/null

man tee:

-a, --append

append to the given FILEs, do not overwrite

Here, we use tee as sudo in order to append to foo, and dump (to /dev/null) the other effect of tee: duplicating the input to the stdout.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
4

Another way to achieve that would be this:

sudo bash -c "cat bar >> foo"