Quickly compare files over SSH using diff

I maintain several Linux servers that provide DNS, web, system monitoring, syslog, and config file archival. These servers have been loyal workhorses that seldom need much care and feeding other than periodic software updates. Over the past several years my confidence and experience with Linux has grown and I’ve attempted to make notes of useful commands that I don’t use very often.

I came across one of these useful commands today and decided to make a simple blog entry if for no other reason than to help remind myself of the syntax.

Nearly every Linux system has the “diff” application available. This application points out the differences (hence the name “diff”) between the input files. I’ve used this command many times to compare files on the same server, but today I wanted to compare two files that were located on different servers. To accomplish this remote file diff operation you can simply use the ssh command and pipe the results to the diff command as follows:

ssh {remote host} cat {remote file patch} | diff {local file path} -

Here’s an example filling in the blanks with actual data:

ssh 10.0.0.2 cat /etc/named.conf | diff /etc/named.conf -

Note the trailing – (hyphen) at the end. That is not a typo.