Mohammed Selman
Mohammed Selman

Reputation: 575

Append content from one file to another in Linux

I have two files, file1 on server 1 and file2 on server 2.

Now I want to write a script to append the contents of file2 (from server 2) to file1 (on server 1), that is, without overwriting the original contents.

How can I do this with a shell script (using Ubuntu Linux)?

Upvotes: 0

Views: 3874

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

ssh server2 "cat /path/to/file2" | ssh server1 "cat >> /path/to/file1"

If minimizing network traffic is an issue, use the trickier-to-quote version:

ssh server2 'cat /path/to/file2 | ssh server1 "cat >> /path/to/file2"'

The first version transfers the file to your local host, then to server1. The second version transfers the file directly from server2 to server1. (If either file path contains spaces, the quoting becomes much trickier.)

Upvotes: 5

Related Questions