Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 another host, also accessible via ssh, without having to manually download the remote file first.

share|improve this question
    
You could mount the remote directory using SSHFS, and then access it as if it's local. –  Barmar Jul 16 at 20:32

3 Answers 3

up vote 10 down vote accepted
ssh user@remote_host "cat remote_file.txt" | diff - local_file.txt

Source

share|improve this answer
    
Won't the pipe just download the results of cat, which are in fact just the original file? –  Stijn de Witt Jul 14 at 18:32
    
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 at 18:52
    
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 at 19:12
    
@StijndeWitt, yes I wanted to avoid having to scp the remote file first and then do the diff.. –  fduff Jul 14 at 19:48

Try:

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

or using vimdiff:

vimdiff local_file scp://server//path/to/remote_file
share|improve this answer
    
vimdiff is also an interesting option! Although the syntax should be: scp://server//path_to_file note the // before the path. –  fduff Jul 14 at 18:48
    
@fduff: Thanks, updated! –  Gnouc Jul 14 at 18:52

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.

share|improve this answer
1  
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 at 18:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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