Run a diff between local and remote files

I’m connected on a host via ssh and I’d like to compare (let’s say with diff) a certain config file against its counterpart on an another host, also accessible via ssh, without having to manually download the remote file first before running the diff.

Asked By: fduff

ssh user@remote_host "cat remote_file.txt" | diff - local_file.txt


Answered By: polym


diff local_file <(ssh user@server 'cat remote_file')

or using vimdiff:

vimdiff local_file scp://server//path/to/remote_file
Answered By: cuonglm

Maybe this is not helpful in your exact case, but I often simply use

sum -r

on each machine, and manually compare the checksums. That allows for comparisons of files even when they are not reachable via an ssh connection. Of course, all it answers is “are they identical”, but often that is sufficient for my needs. It also makes it easy to verify that a single file is the same on 3 or 4 or more machines.

Answered By: MJB

If you’d prefer a more visual approach in you terminal session. The midnight commander has a remote file system option (SFTP link… option in the menu) and visual file compare option. It is not installed by default on most Linux systems but is available in most base repositories. Steps:

  1. Start midnight commander (command: mc) in the folder containing the file to be compared
  2. On other side (for example the Right menu) create a sftp link to the other server at the same folder (sftp://<user>@<servername>/<path>).
  3. Using the ins key select/highlight the files to be compared. (tab changes between left and right panel).
  4. In the Command menu, select Compare files

Note: The midnight commander menu can usually be access using the mouse in your terminal session. It that doesn’t work F9 gets you in the menu also.

Answered By: Fholst

ssh user@remote_host "cat remote_file.txt" | cmp local_file.txt

or if you don’t want to show result, can run in silent mode

ssh user@remote_host "cat remote_file.txt" | cmp -s local_file.txt

Answered By: Joey2019
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.