Script to automate file comparisons


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to automate file comparisons
# 1  
Old 03-16-2005
Script to automate file comparisons

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!
# 2  
Old 03-16-2005
this will work for ksh or bash
or ksh replace bash with ksh on the first line

create a script dircmp.sh as follow:
Code:
#!/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>
# 3  
Old 03-16-2005
Most versions of diff will compare two directories. Check the man pages for your system.
E.g.: diff --brief dir1 dir2
# 4  
Old 03-16-2005
sort
and then
comm
# 5  
Old 03-16-2005
Quote:
Originally Posted by ZealeS
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.
# 6  
Old 03-17-2005
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!
# 7  
Old 03-17-2005
As I said, most versions of diff will compare two directories. Check the man pages for your system, i.e. HP-UX...
Quote:
SEE ALSO
bdiff(1), cmp(1), comm(1), diff3(1), diffmk(1), dircmp(1), ed(1),
more(1), nroff(1), rcsdiff(1), sccsdiff(1), sdiff(1), terminfo(4).
So maybe you should look at using the dircmp command?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File comparisons

Hi all, I want to compare two files based on column value Kindly help me a.txt 123,ABCD 456,DEF 789,SDF b.txt 123,KJI 456,LMN 321,MJK 678,KOL Output file should be like Common on both files c.txt 123,ABCD,KJI (8 Replies)
Discussion started by: aaysa123
8 Replies

2. UNIX for Dummies Questions & Answers

File and if statement comparisons

I'd love to get help on this one please. Ok so say I have a file called README with lines such as this: index:index.html required:file1.1:file2.1:file3.1 I'm having trouble with writing an if statement that compares the items in a list with a file inside README, what I imagine in my head... (7 Replies)
Discussion started by: mistsong1
7 Replies

3. Windows & DOS: Issues & Discussions

automate the script

Dear all, I I want to login to my Linux machine using putty and then run some script from Windows machine.we can do it after loging it and then execute the script by typing it in putty command line screen. but I want to automate it.So whenever I will fire this script,it will do the following... (4 Replies)
Discussion started by: smartgupta
4 Replies

4. Shell Programming and Scripting

Automate remote script

Hi all, I need to execute a script on a remote machine that are connected to the network.The basic requirement is to write a script which will login in remote machine and then execute the other script automatically placed in remote machine.So that I need to execute the remote machine script... (3 Replies)
Discussion started by: smartgupta
3 Replies

5. Shell Programming and Scripting

mail script to automate

Hi, Here below the logs from the mail server: less /var/log/messages: Sep 6 04:03:31 server-59 out: 1252227811|webmaster@zilia.com|antonino.granata@gmail.com|2175|success|1 Sep 6 04:03:33 server-59 in: 1252227813|news@tarot.com|junk@thess.com|30376|success|1 Sep 6 04:03:35 server-59... (8 Replies)
Discussion started by: gsiva
8 Replies

6. Shell Programming and Scripting

How can I automate a script?

Hi All, Can I automate a script when some one trying to 'vi' (open) a file. For Example, I am having a file named 'SecuredShell.sh'. when a user types " vi SecuredShell.sh " in unix command prompt a script named secure.sh needs to be automated. Can this be possible. if Yes please guide... (2 Replies)
Discussion started by: little_wonder
2 Replies

7. Shell Programming and Scripting

Looking for help with a script to automate VLC

Hi, New member here looking for help. This might not be a post for the 'VERY basics' section, so feel free to move it to somewhere more appropriate. I've created a script that searches my computer for video files, creates a list of these files, and selects a number of random entries to play in... (2 Replies)
Discussion started by: uncertain
2 Replies

8. Shell Programming and Scripting

Automate shell script

I would like to automate script where i do not have to manually insert the username and password I wrote two different scripts but not able to achieve the results: here's to scripts i wrote #!/bin/bash cd /var/tmp /home/server/steve/pca --askauth -idx /opt/app/bin/expect <<EOF expect... (3 Replies)
Discussion started by: sam786
3 Replies

9. Shell Programming and Scripting

here document to automate perl script that call script

I am trying to use a here document to automate testing a perl script however when the perl script hits a system(perl subscript.pl) call, input is no longer entered into this subscript. here is my script $ cat test.sh #ksh for testcase do program <<-EOF | tee -a funcscnlog.log y... (3 Replies)
Discussion started by: hogger84
3 Replies

10. UNIX for Dummies Questions & Answers

automate the input in a script

I have a program that i have to run by cron. The program needs user input. So i have to automate that in a littke script. start of script program.sh: result=program.log; export result echo Program starting : `date` >> $result /usr/local/program >> $result echo Program running : `date` >>... (11 Replies)
Discussion started by: erwinspeybroeck
11 Replies
Login or Register to Ask a Question