Diff’ing Some Files Across Similar Directory Trees

The “diff” command is a very nice tools in *NIX environments to compare the content of two files. But there are some situations where diff is a pain to use. The classic case is when you need to compare many files from different directory trees (by example two different releases of a tool) located in the following directories:

/data/tool-1.0/
/data/tool-2.0/

Diff has a feature to display differences between files in a recursive way:

diff -r /data/tool-1.0/ /data/tool-2.0/

Nice but not convenient to just compare some files when browsing the directory tree. Example, if I need to compare foo.h between the two trees:

$ cd /data/tool-2.0/src/include
$ diff foo.h /data/tool-1.0/src/include/foo.h
1c1
< Line1

> Line2

When you need to compare many files, it’s a pain to always type long path (yes, I’m a lazy guy). I write this quick & dirty script to allow me to compare files between two directory trees without re-typing paths all the time:

#!/bin/bash
test -z $SOURCEPATH && echo "\$SOURCEPATH not defined" && exit 1
test -z $DESTPATH && echo "\$DESTPATH not defined" && exit 1
NEWPATH=`echo $PWD | sed -e "s~$SOURCEPATH/~$DESTPATH/~"`
FILENAME=${@: -1}
NARGS=$(($#-1))
ARGS=${@:1:$NARGS}
diff $ARGS $PWD/$FILENAME $NEWPATH/$FILENAME

Create your two environment variables and just type the filename you want to compare:

$ export SOURCEPATH=/data/tool-2.0
$ export DESTPATH=/data/tool-1.0
$ cd $SOURCEPATH/src/include
$ mydiff foo.sh
1c1
< Line1

> Line2

Note that args passed via the original command line as passed to diff in the script. As I said, quick & dirty…

[The post Diff’ing Some Files Across Similar Directory Trees has been first published on /dev/random]

Article Link: https://blog.rootshell.be/2020/02/23/diffing-some-files-across-similar-directory-trees/