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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a shell script to compare two directories and produces the output
# 1  
Old 03-27-2012
Bug 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.

Last edited by DukeNuke2; 03-27-2012 at 08:11 AM..
# 2  
Old 03-27-2012
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...
# 3  
Old 03-28-2012
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?
# 4  
Old 03-28-2012
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?
# 5  
Old 03-28-2012
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.
# 6  
Old 03-28-2012
bash

Hi,

Try this one,

Code:
#! /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,
RangaSmilie
# 7  
Old 03-28-2012
In solaris a simple command "diff -r" would be suitable for your kind of requirement.

Try this out:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk runs and produces output but with error

When I run the awk below, I get an error message awk -v OFS='\t' '$(NF-1)=="Benign" || ($(NF-2) OFS $(NF-1))=="Likely Benign" {$(NF)=$(NF-2) OFS $(NF-1)} {print $0 }' input awk: cmd. line:1: (FILENAME=VUS FNR=8) fatal: attempt to access field -1 input Chr Start End Ref ... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Need shell script to compare directories and delete files on target server

Hello, I need help in writing the shell script for below mentioned case. There are 2 servers(server A, server B). A cronjob syncs files between these 2 servers. Existing script is copying files from A to B. This is done using the command rsync. However, the files are not deleted... (2 Replies)
Discussion started by: SravaniVedam11
2 Replies

3. Shell Programming and Scripting

Compare & Copy Directories : Bash Script Help

Beginner/Intermediate shell; comfortable in the command line. I have been looking for a solution to a backup problem. I need to compare Directory 1 to Directory 2 and copy all modified or new files/directories from Directory 1 to Directory 3. I need the directory and file structure to be... (4 Replies)
Discussion started by: Rod
4 Replies

4. UNIX for Dummies Questions & Answers

linux sort command produces strange output

cat a .a ba .b bb .c bc sort a .a .b ba bb bc .c NOTE: .a and .b appears before ba and bb, where as .c appears after bc. In general (3 Replies)
Discussion started by: ajb
3 Replies

5. Homework & Coursework Questions

2. Write a shell script that produces some summary information of the system at a particular moment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: 3. The attempts at a solution (include all... (2 Replies)
Discussion started by: jsk319342
2 Replies

6. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

7. UNIX for Dummies Questions & Answers

gzip produces different output from the same input

Hi there, I'm puzzled. Compressing the same file (same name, same md5sum) at two different times will produce a different output. I mean the md5sum of the resulting .gz files are different. Does it make any sens to any of you? I'd like some explanations if you know what's going on. Thanks... (4 Replies)
Discussion started by: chebarbudo
4 Replies

8. Shell Programming and Scripting

read -n1 -r -p "Press..." key / produces error in bash shell script

Hello! Sorry, for my not so perfect english! I want to stop bash shell script execution until any key is pressed. This line in a bash shell script read -n1 -r -p "Press any key to continue..." key produces this error When I run this from the command line usera@lynx:~$ read... (4 Replies)
Discussion started by: linuxinho
4 Replies

9. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

10. Linux

vgscan produces no output using file descriptors on Oracle Enterprise Linux.

I wrote a simple program which will create a child process to execute a command and the output will be redirected to the file. Please have a look at the following code -> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> void execute(char **argv) { ... (0 Replies)
Discussion started by: sandiworld
0 Replies
Login or Register to Ask a Question