Need a shell script to compare two directories and produces the output

Hi,
I am using solaris OS 10 and Bash shell.I need a script which will compare the two directories and produces the output.
Step 1: In detail say suppoose I have machine one and have a directory dir1. Script should iterate through the directories and subdirectories inside and produce the output say output1.txt.

second step: I have a machine 2 and directory say dir2.script again iterate through the directories and subdirectories inside and produce an output say output2.txt

third step: need to compare the two files say output1.txt and output2.txt and if you found any difference in the two output files generate the output to new text file say output3.txt.

I am new to shell scripting. Appreciate your help on it.

for a start i would try to find out how to do the above manualy with commands like find and diff . after knowing how this works you can start to automate the procedure in a script...

ok thanks

---------- Post updated 03-28-12 at 12:29 AM ---------- Previous update was 03-27-12 at 06:19 AM ----------

hi any other help on it?

Do the two directories have the same files?

And what exactly are you trying to differentiate? the list of files or the contents of each file?

Hi two directory names are same but residing in two different servers.
The two directories having different files and directories inside.Some of the directories and files are same and rest of them are different.

My intention is to compare the list of the files and directories in the server1 dir1 output file with server2 dir1 output file.
finally to find out the missing directories and files between two output files.
yes i need only list of the files.Another concern is there are some softlinks inside the directories not sure how to verify them.

Advance thanks for your response.

Hi,

Try this one,

#! /usr/bin/bash
if [ $# -ne 2 ];then
echo "Usage: $0 -l [location] -o [outputfile]";
exit;
fi
dir=$1
output=$2
cd $dir
rm $output
for i in `find $dir -name "*"`
do
    if [ -d "$i" ];
    then
        echo "DIR:$i" >>$output
        continue
    fi
    echo $i >>$output
done

Execute Procedure:
script1.sh location outputfile

This script gets all the type of files and directory in the location which you have to give through command line.

This script should be configured in cron in both the servers and then these two output files can be compared through ssh.

Cheers,
Ranga:)

In solaris a simple command "diff -r" would be suitable for your kind of requirement.

Try this out:

diff -r dir1 dir2

This will recursively search and compare both the directories as well as its files. It will also show the files which are only in dir1 or in dir2 which is what you want. Instead of taking the output of directory link in a file and comparing it - I would suggest if possible mount any one dir in another server and run the diff from there.

Hi Thanks for the response.

@suhasingale:yeah that's correct but problem is we cannot connect to the other server because it is in different network.
what is my plan is develop a script and execute it first our envrionment and will send the script to the other server owner and ask them to send the out put file after running the same script.from there will compare two files outputs.

@rangarasan: Can we get the count also at the end to get an idea of the how many files are present in the perticular directory.

ok - I understand now. You will have to write a script to compare two file in two ways then.

Yes, You can :slight_smile:

Try this one,

#! /usr/bin/bash
if [ $# -ne 2 ];then
echo "Usage: $0 [location] [outputfile]";
exit;
fi
dir=$1
output=$2
filecnt=0
dircnt=0
cd $dir
rm $output
for i in `find $dir -name "*"`
do
    if [ -d "$i" ];
    then
        echo "DIR:$i" >>$output
        let dircnt++
        continue
    fi
    echo $i >>$output
    let filecnt++
done
echo "File Count:$filecnt" >>$output
echo "Directory Count:$dircnt" >>$output

Cheers,
Ranga:)

1 Like

Hi.

If your system has rsync, I'd use that in the dry-run mode. That would tell what would need to be changed to put the directories in sync. That may or may not be a sufficient report for your purposes, but it would be relatively easy to do (although there is a myriad of available options for rsync) , and could be used as a source for comparison if you had to write a script.

Ir was not installed by default on one Solaris 10 (386) system that I use, but it was on an OpenIndiana box.

Otherwise, it seems to me that you'd need to send data back in a kind of skeleton form, which would describe the directory structure, along with a checksum of the files, and do the compare based on the checksums calculated for the local files ... cheers, drl