57

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.

1
  • You could mount the remote directory using SSHFS, and then access it as if it's local.
    – Barmar
    Jul 16, 2014 at 20:32

5 Answers 5

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

Source

5
  • 1
    Won't the pipe just download the results of cat, which are in fact just the original file? Jul 14, 2014 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, 2014 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. :) Jul 14, 2014 at 19:12
  • @StijndeWitt, yes I wanted to avoid having to scp the remote file first and then do the diff..
    – fduff
    Jul 14, 2014 at 19:48
  • Simple diff works, but can we do it with vimdiff?
    – Chan Kim
    Aug 8, 2023 at 8:56
34

Try:

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

or using vimdiff:

vimdiff local_file scp://server//path/to/remote_file
5
  • 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, 2016 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, 2016 at 11:51
  • vimdiff password prompt is ok while password prompt with diff leads to empty remote side
    – monok
    May 15, 2020 at 15:25
  • vimdiff one is awesome ! Mar 8, 2021 at 14:51
  • vimdiff doesn't work for me. What is the exact command? I tried vimdiff test1 scp:[email protected]://home/ckim/bin/test1. The right side is empty. Of course if I do scp [email protected]://home/ckim/bin/test1 test2, the file is copied to test2 ok.
    – Chan Kim
    Aug 8, 2023 at 8:54
13

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.

2
  • 3
    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. Jul 14, 2014 at 18:34
  • 2
    Commanding info coreutils 'sum invocation' says, inter alia: "'sum' is provided for compatibility; the 'cksum' program (see next section) is preferable in new applications." Nov 22, 2018 at 7:46
4

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.

1
  • Does not work: "/sftp://live_pr~d/asset/static" is a directory Apr 17, 2023 at 9:47
1

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

1
  • 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, 2020 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .