0

I hope the title makes sense.

I am trying to copy the last line of a file onto a file with same name on a remote server.

For example: copy last line of server1:/opt/users.txt to server2:/opt/users.txt

server1: cat /opt/users.txt

  1. john
  2. Kathy
  3. Sierra

server2: cat /opt/users.txt

  1. john
  2. Kathy

copy last line of server1:/opt/users.txt to server2:/opt/users.txt

output

server2: cat /opt/users.txt

  1. John
  2. Kathy
  3. Sierra
1
  • How do you access the server? Is the server file always equal to the local file, except for the last line? If yes, that would be simplified to "copy a file to a server.
    – Quasímodo
    Jan 12, 2021 at 20:15

1 Answer 1

4

From server1, assuming that user has appropriate permissions to write to the file:

tail -n 1 /opt/users.txt | ssh user@server2 'cat - >> /opt/users.txt'
1
  • Thank you @steeldriver. I have been working on that this morning and could not make it work. This is exactly what i was looking for. Cheers!!
    – Legio06
    Jan 12, 2021 at 20:43

You must log in to answer this question.

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