LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2009, 03:55 PM   #1
fpp666
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Rep: Reputation: 0
How to compare files in 2 dirs and write results on a file


Hi, I need to write a script using ksh that compares files from two directories.

I need to do a comparison between historic output files from a program and the new output files of the same program, to verify that changes made to the code did not affect the overall result of it.

The original files and the new files have the same name, but the new files have an additional .rdy at the end. Other than that, the files are the same, but have to be compared to the corresponding original file.

The result of this has to be written in a file, for an easier verification.

Hope you can give me a hand, thanks in advance!
Fernando
 
Old 07-12-2009, 04:11 PM   #2
corbintechboy
Member
 
Registered: Sep 2003
Location: Kentucky
Posts: 480
Blog Entries: 1

Rep: Reputation: 51
Check out the command diff.
 
Old 07-12-2009, 05:53 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by corbintechboy View Post
Check out the command diff.
And/or 'cmp'.
 
Old 07-13-2009, 08:44 AM   #4
fpp666
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Ok, I've been reading and I think I get how diff works now, but I have a doubt about how to diff a bunch of files.

Should I just list all the diff commands for the files I need to compare one after another, or maybe put them on a list and get the script to pick them up from there?

Sorry about all the questions, I'm really new to UNIX.

Thanks!
 
Old 07-13-2009, 08:52 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If the files had the same name, you could have recursively compared the content of two directories using option -r of diff. In your case you can use a loop like this:
Code:
#!/bin/bash
dir_old=/path/to/dir/containing/original/files
dir_new=/path/to/dir/containing/new/files
cd $dir_old
for name in *
do
  echo diff "$dir_old/$name" "$dir_new/$name.rdy" >> logfile
  diff "$name" "$dir_new/$name.rdy" >> logfile
done

Last edited by colucix; 07-14-2009 at 11:25 AM. Reason: forgotten slashes between dirname and filename
 
Old 07-14-2009, 01:58 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See also 'comm' http://linux.die.net/man/1/comm
 
Old 07-14-2009, 02:26 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
use diff -r
It will handle sub directories as well

Code:
 diff -r dir1 dir2 > files.diff
 
Old 07-14-2009, 10:53 AM   #8
fpp666
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for the responses, guys!

One thing I forgot to mention: The original output files are all together in the same directory, but the newly generated files are separated in sub directories (e.g. newfiles/switch1, newfiles/switch2).

How can I make the script look recursively in one side of the diff, and always on the same dir for the other half of the diff sentence.

I tried what Colucix suggested, using one of the subdirs as new_dir, but I get a huge output dir, because the script tries to compare the complete set of original files (about 120) with the 3 that are located in that subdir.

I really appreciate your help, cheers!
 
Old 07-14-2009, 11:31 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by fpp666 View Post
One thing I forgot to mention: The original output files are all together in the same directory, but the newly generated files are separated in sub directories (e.g. newfiles/switch1, newfiles/switch2).
Keep it simple and put the new files in one directory and keep the original names without the suffix .rdy? In that case you can use diff -r.

If you like to make things complicate... you have to use the find command to locate where the corresponding new file is. For example something like this:
Code:
#!/bin/bash
dir_old=/path/to/dir/containing/original/files
dir_new=/path/to/dir/containing/subdirs/with/new/files
cd $dir_old
for name in *
do
  newfile=$(find $dir_new -name ${name}.rdy)
  echo diff "$dir_old/$name" "$newfile" >> logfile
  diff "$name" "$newfile" >> logfile
done
 
Old 07-15-2009, 12:27 PM   #10
fpp666
LQ Newbie
 
Registered: Jul 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Excellent advice, guys, I really appreciate it!

Cheers!
Fernando
 
  


Reply

Tags
file, ksh



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to let cdda2wav write files and dirs with cddb names? darkleaf Linux - Software 5 10-11-2004 03:34 PM
ls, dirs first, files later TroelsSmit Linux - Newbie 4 05-31-2004 11:47 AM
Write results to log? eXor Linux - Software 2 06-12-2003 01:18 PM
how to make my account on proftpd to be able to see all dirs and able to write. Mouse_103 Linux - Newbie 2 04-02-2003 07:07 PM
How Compare FS/dirs - can't use 'diff'? MikHud Linux - General 2 05-07-2002 07:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration