How to compare file between local server and remote server over SSH



How to compare file between local server and remote server over SSH

There are some ways to compare file between local server and remote server, but some ways only tells us whether file contents is different and doesn't tell us what is different in file contents.

In this article, we are going to use ssh and diff commands to get difference of file contents between local and remote servers.

We have the following file in local server and remote server (README.txt). Only third line is different.
[Local server]
$ cat README.txt
11111
22222
33333
44444
55555

[Remote server]
$ cat README.txt
11111
22222
99999
44444
55555


Before comparing the files between local and remote servers through SSH, let's check whether we can get remote server's file contents as follows.
$ ssh username@10.10.10.2 'cat /tmp/README.txt'
username@10.10.10.2's password: 
11111
22222
33333
44444
55555


Once we made sure that we can see remote server's file contents through SSH, we can compare the file as follows. The point is that we can use remote server's output in the diff command ("-" means standard input). As a result of that, we can compare remote server's file contents.
$ ssh username@10.10.10.2 'cat /tmp/README.txt' | diff -u - README.txt
username@10.10.10.2's password: 
--- -   2017-11-01 23:32:46.489667911 +0900
+++ README.txt  2017-11-01 23:29:52.153271108 +0900
@@ -1,6 +1,6 @@
 11111
 22222
-99999
+33333
 44444
 55555