Solved comparing files in two directories

July 29, 2012 at 22:32:11
Specs: Windows XP
Hi

I'm want to compare files in one directory with files in another directory say:

i have two directories one and two, structure as below:

one
|--apps
|--sql


two
|--apps
|--sql


I'm tring to write a script to drill down the a directory (say "one" in this case) and get the file name with the path. then check if that file exists in another directory ( say "two" in this case) and if it exists get the version of the files into a text file.

i'm currently doing it using the below command to get the list of files in a given directory and sub directories and then doing a manual check on the files:

find <directory_name> -exec grep -il ' ' /dev/null {} \;>>out.txt

wanted to automate this process, didnt know how to start about, need your help to meet this.

Thanks in advance.

Rajz


See More: comparing files in two directories

Report •

#1
July 30, 2012 at 16:12:52
✔ Best Answer
This is one way of doing it. I decided to place the contents of the "two" directory in a text file. I also placed the file name as the second field so I can readily compare file names.

There is one limitation: No spaces are allowed in file names or directory names:

#!/bin/ksh

d2="/home/nails/sandbox/sct30/two"
df="./datafile.txt"
of="./out.txt"

rm -f $df

# create a file with the "two" directory file contents
find "$d2" -type f -print| while read twofile
do
   # strip off the directory listing
   f2=${twofile##*/}
   echo "$twofile $f2" >> "$df"
done

d1="/home/nails/sandbox/apps/one"
# search the "one" directory
find "$d1" -type f -print| while read onefile
do
   v2=${onefile##*/}
   # if the file in the "one" directory exists in the "two"
   # directory, echo it to the out file.
   while read dir1 file2
   do
      if [[ "$v2" == "$file2" ]]
      then
          echo "$dir1" >> "$of"
      fi
   done < "$df"
done


Report •

#2
July 30, 2012 at 21:21:09
Hi Nails,

Thank you for the replay would try it out..
But in the mean time i had tried out some thing as below:
You have any suggestions on this one..

for your reference the input file would have these values:
Got the infile after running this command out
find <directory_name> -exec grep -il ' ' /dev/null {} \;>>4194693.txt

==============
In File 4194693.txt
==============
fnd/patch/115/import/afmdmsg.lct
ont/forms/US/OEXOEBSO.fmb
ont/forms/US/OEXOETEL.fmb
ont/forms/US/OEXOEORD.fmb
ont/patch/115/odf/ontseq.odf
ont/patch/115/odf/ontvws.odf
ont/patch/115/odf/onttab.odf
ont/patch/115/sql/OEXLLINB.pls
ont/patch/115/sql/OEXUSCHS.pls
ont/patch/115/sql/OEXFHDRS.pls


=======
Program
=======

#!/bin/ksh

ONE_DIR="/var/tmp/rajz/4194693"
U_TOP="/xnappi/applmgr/1159"
P_FILE="4194693.txt"
echo "Dir One File Name , Dir One File Version , Dir Two File Name,Dir Two File Version , Dir One file Path , Dir Two File Path Path , Matched/Not Matched"
while read f
do
filename=`basename $f`
xtn=`echo $f|cut -f2 -d.`
#echo $xtn
pfversion=`adident Header $ONE_DIR/$f|tail -1|cut -f2,3 -d" "`
pf=`adident Header $ONE_DIR/$f|tail -1|cut -f3,3 -d" "`
ax=`echo $f|cut -f1 -d"/"`
ap=`echo $f|cut -f1 -d"/"| tr '[:lower:]' '[:upper:]'`
ap=$(unalias echo; eval echo $"${ap}_TOP")
af=`echo $f|sed "s/$ax//"`
if [ "$xtn" = "fmb" ] ; then
ap=$AU_TOP/forms/US
af="/$filename"
fi

if [ "$xtn" = "pll" ] ; then
ap=$AU_TOP/resource
af="/$filename"
fi

ufversiopn=`adident Header $ap$af|tail -1|cut -f2,3 -d" "`
uf=`adident Header $ap$af|tail -1|cut -f3,3 -d" "`
if [ "$pfversion" = "$ufversiopn" ] ; then
#echo "$pfversion , $ufversiopn ,$ONE_DIR/$f ,$ap$af , Matched"
echo "$filename ,$pf, $filename ,$uf,$ONE_DIR/$f ,$ap$af , Matched"
else
# echo "$pfversion , $ufversiopn ,$ONE_DIR/$f ,$ap$af , Not Matched"
echo "$filename ,$pf, $filename ,$uf,$ONE_DIR/$f ,$ap$af , Not Matched"
fi;
done < $P_FILE


Report •
Related Solutions


Ask Question