Hi, I need a script that loops through all the files two directories
passed to it via parameter, and if two files have the same name, do a
cmp comparison on the files. If the files are different, output the
specifics returned by cmp. What's the best way to go about writing
this, as I am a scripting newbie? Thanks!
this will work for ksh or bash
or ksh replace bash with ksh on the first line
create a script dircmp.sh as follow:
#!/bin/bash
COMM=/usr/bin/comm
CMP=/usr/bin/cmp
DIR1=$1
DIR2=$2
ls ${DIR1} > /tmp/DIR1.$$
ls ${DIR2} > /tmp/DIR2.$$
COMMON_FILES=$(${COMM} -1 -2 /tmp/DIR1.$$ /tmp/DIR2.$$)
for file in ${COMMON_FILES}; do
echo comparing ${DIR1}/${file} with ${DIR2}/{file}
${CMP} ${DIR1}/${file} ${DIR2}/{file}
done
rm /tmp/DIR1.$$
rm /tmp/DIR2.$$
adjust
COMM=/usr/bin/comm
CMP=/usr/bin/cmp
as appropriate
The invoke the script as ./dircmp.sh <dir1> <dir2>
Most versions of diff will compare two directories. Check the man pages for your system.
E.g.: diff --brief dir1 dir2
sort
and then
comm
There is no need to sort when using comm, and it will yield no benefit.
Ygor's way is the way I would do things, if you don't *really* need to use cmp, I would suggest you use that.
Hi Ygor, it seems the diff command gives me everything I need. However, I noticed that it dumps the differences of the files, resulting in a lot of needless output. Is there a way to turn that off? The --brief flag doesn't work in our distrubution (HP-UX). Thanks!
As I said, most versions of diff will compare two directories. Check the man pages for your system, i.e. HP-UX...
So maybe you should look at using the dircmp command?