HackDig : Dig high-quality web security articles for hackers

Diff’ing Some Files Across Similar Directory Trees

2020-02-23 08:59

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]


Source: /seert-yrotcerid-ralimis-ssorca-selif-emos-gniffid/32/20/0202/eb.llehstoor.golb

Read:474 | Comments:0 | Tags:Unix

“Diff’ing Some Files Across Similar Directory Trees”0 Comments

Submit A Comment

Name:

Email:

Blog :

Verification Code:

Announce

Share high-quality web security related articles with you:)

Tools

Tag Cloud