20

Can I use the diff command to compare files on two different servers? If not, is there any other option?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Anusha Pachunuri
  • 1,389
  • 4
  • 18
  • 39

5 Answers5

31

You can copy the file over from the other server using scp and then use diff.

Or ssh to the remote host and diff using a single command like this:

ssh user@remote "cat /path/to/remote/file" | diff - /path/to/local/file
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • Just wanted to make sure if the first solution is an optimized solution when we are trying to compare bulk files on both servers? Wouldn't it load the source server with the files copied from the destination server? – Mukul Chikhalkar Aug 08 '22 at 07:09
11

I know it's a late answer but I take the question literally, no local file and two remote files.
In bash (and not only) it's possible to use the process substitution [1,2] <(...):

diff <(ssh Server1 'cat /path/to/file1') <(ssh Server2 'cat /path/to/file2')

The process <(list) is run asynchronously, and its input or output appears as a filename.

Note

  • Of course if you need only one remote file you can put the local file instead one of the <(...).
  • If both files are on the same server you can use a simpler

    ssh Server1 'diff /path/to/file1 /path/to/file2'
    
Hastur
  • 2,470
  • 27
  • 36
11

If your comparing multiple files, then look up rsync and rdiff, which save you the bandwidth of copying all files.

Btw, if your files are very large, then please update your question with that information.

Jeff Burdges
  • 4,204
  • 23
  • 46
5

The "-" diffs against STDIN. You can do something like this:

ssh server 'cat file_to_diff' | diff -u localfile -
Corey Henderson
  • 7,239
  • 1
  • 39
  • 43
0

If you're troubleshooting AWS instance then it may make sense to stop inatanceA then attach its drive to instanceB you're want to compare with.

Putnik
  • 5,925
  • 7
  • 38
  • 58