reli
12/2/2013 - 9:49 AM

diff

diff

Ever wanted to do a diff between a local file and a remote one?  I have, so I wrote a simple shell script that'll do it for you.  It requires scp, diff, and rm to be available on your system, which should be the case on any modern *nix.  The -b option to diff tells it to ignore white-space difference.  If you don't have password-less SSH authentication set up, you'll want to remove the >& /dev/null trailers from the two scp lines so you get your prompts.

To run, just drop this into /usr/bin (or somewhere else on your path), make it executable, and call it just like diff, except with scp-compatible file paths.

#!/bin/sh
#
# this acts as a remote diff program, accepting two files and displaying
# a diff for them.  Zero, one, or both files can be remote.  File paths
# must be in a format `scp` understands: [[user@]host:]file

if [ "$1" = "" -o "$2" = "" ]; then
echo "Usage: `basename $0` file1 file2"
exit 1
fi

scp $1 rdiff.1 >& /dev/null
scp $2 rdiff.2 >& /dev/null
diff -b rdiff.1 rdiff.2
rm -f rdiff.1 rdiff.2
Update: Horst and Amit  both posted a far better solution that leverages SSH directly.  Back when I wrote this I didn't appreciate SSH's power for more than interactive shells.  Here's his command (replaced the brace-wrapped strings as needed):

ssh {remote_host} cat {remote_file} | diff {local_file} -