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
  • 617
  • 6
  • 14

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
  • 66,199
  • 35
  • 114
  • 250
4

Another way to achieve that would be this:

sudo bash -c "cat bar >> foo"
George Udosen
  • 1,807
  • 13
  • 26