remdiff -- a remote diff'er

Want to know the changes between two files on your computer? "diff" is a wonderful command.

But what if one or both of those files are on a different machine? Usual response would be to copy the file(s) to your local machine, then run the diff. And how often have you accidently overwritten your local copy because your fingers got ahead of your brain?

And that's why I wrote "remdiff". I consider this a pretty important script in my toolkit. You want this on your disk-to-disk backup system. Really, you do.

========================================================
#!/bin/ksh

yell() { echo "$0: $*" >&2; }
die() { yell "$*"; rm -f /tmp/$$.*; exit 111; }
try() { "$@" || die "cannot $*"; }

/*
 * Copyright (c) 2016 Nicholas Holland 
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

if [[ -z "$2" ]]; then
    cat <<- _ENDUSAGE
	usage:
	    $0 file1 file2
	where either one can be a remote file."
	_ENDUSAGE
    exit
fi

# This will fail if the fileNAME has a : in it. Oh well.
if echo $1 |grep -q ":"; then
    try scp -q $1 /tmp/$$.file1
    file1=/tmp/$$.file1
else
    file1=$1
fi

if echo $2 |grep -q ":"; then
    try scp $2 /tmp/$$.file2
    file2=/tmp/$$.file2
else
    file2=$2
fi
 
diff -u $file1 $file2
 
rm -f /tmp/$$.*
========================================================
Put this in your path.

Usage

$ remdiff localfile server:/path/to/file
or
$ remdiff server:/path/to/file localfile
or
$ remdiff server1:/path/to/file1 server2:/path/to/file2

How it works:

A token effort is made to figure out if a file is specified in host:path/file format. If so, the file is copied over to a local tmp file. Same takes place for the other file. Once both files are in a known, local place, a diff -u is run.

Tested on AIX and OpenBSD, will probably work on most Unixes with nothing more than maybe fixing the "#!/bin/ksh" line to point to your sh-compatible shell.

Limitations:

Only limited error checking is done using the TryYellDie code. But then, newbie users probably won't be doing diffs.

Files with a ':' in the file name will be handled incorrectly.

The files should be modest in size -- don't fill your /tmp partition!

It is possible to do this without a tmp file at all, but I haven't figured out a way to handle TWO remote files without tmp files. Basically, cat the remote file to stdout, and diff uses '-' as one of its input files. I find it more useful to be able to diff two remote files than huge files I don't want to copy locally.
 
 

Holland Consulting home page
Contact Holland Consulting
 

since June 20, 2021

Copyright 2020, Nick Holland, Holland Consulting.