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.

  • You could mount the remote directory using SSHFS, and then access it as if it's local. – Barmar Jul 16 '14 at 20:32
ssh user@remote_host "cat remote_file.txt" | diff - local_file.txt


| improve this answer | |
  • 1
    Won't the pipe just download the results of cat, which are in fact just the original file? – Stijn de Witt Jul 14 '14 at 18:32
  • 1
    the pipe will push the result of the remote ssh command to the diff which is waiting to compare stdin with localfile. – fduff Jul 14 '14 at 18:52
  • 1
    Maybe I'm misinterpreting the 'without having to download the remote file' requirement. I guess OP means 'without having to manually download the remote file'. :) EDIT: Ah, you are the OP. :) – Stijn de Witt Jul 14 '14 at 19:12
  • @StijndeWitt, yes I wanted to avoid having to scp the remote file first and then do the diff.. – fduff Jul 14 '14 at 19:48


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

or using vimdiff:

vimdiff local_file scp://server//path/to/remote_file
| improve this answer | |
  • Does diff local_file <(ssh user@server 'cat remote_file') work only for passwordless SSH? I don't seem to be able to get a password prompt with it... – sdbbs Jun 30 '16 at 8:41
  • 1
    @sdbbs: Process substitution ran in background, and standard output is written to the named pipe (or anonymous named pipe), so you don't get the password prompt. Try cat <(ssh user@server 'cat remote_file') and cat < <(ssh user@server 'cat remote_file') to see the differences. – cuonglm Jun 30 '16 at 11:51
  • vimdiff password prompt is ok while password prompt with diff leads to empty remote side – monok May 15 at 15:25

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.

| improve this answer | |
  • 2
    I can imagine that for large files, or slow connections, this might be very useful. It saves the download of the entire file for when files are actually different. A better Bash scripter than myself can probably combine this answer with the above answers involving a remote cat to only actually run the cat command when it has been determined that the files differ. – Stijn de Witt Jul 14 '14 at 18:34
  • 1
    Commanding info coreutils 'sum invocation' says, inter alia: "'sum' is provided for compatibility; the 'cksum' program (see next section) is preferable in new applications." – Teemu Leisti Nov 22 '18 at 7:46

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.

| improve this answer | |

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

| improve this answer | |
  • cmp does a quick diff and stops as soon as a difference is detected and won't show what exactly changed. So it's not very useful in this scenario. – fduff Oct 6 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.