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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-12-2009, 03:55 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 10
Rep:
|
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
|
|
|
|
07-12-2009, 04:11 PM
|
#2
|
|
Member
Registered: Sep 2003
Location: Kentucky
Posts: 478
Rep:
|
Check out the command diff.
|
|
|
|
07-12-2009, 05:53 PM
|
#3
|
|
Senior Member
Registered: May 2005
Posts: 4,392
|
Quote:
Originally Posted by corbintechboy
Check out the command diff.
|
And/or 'cmp'.
|
|
|
|
07-13-2009, 08:44 AM
|
#4
|
|
LQ Newbie
Registered: Jul 2009
Posts: 10
Original Poster
Rep:
|
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!
|
|
|
|
07-13-2009, 08:52 AM
|
#5
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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
|
|
|
|
07-14-2009, 01:58 AM
|
#6
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,973
|
|
|
|
|
07-14-2009, 02:26 AM
|
#7
|
|
Member
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381
Rep:
|
use diff -r
It will handle sub directories as well
Code:
diff -r dir1 dir2 > files.diff
|
|
|
|
07-14-2009, 10:53 AM
|
#8
|
|
LQ Newbie
Registered: Jul 2009
Posts: 10
Original Poster
Rep:
|
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!
|
|
|
|
07-14-2009, 11:31 AM
|
#9
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
Quote:
Originally Posted by fpp666
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
|
|
|
|
07-15-2009, 12:27 PM
|
#10
|
|
LQ Newbie
Registered: Jul 2009
Posts: 10
Original Poster
Rep:
|
Excellent advice, guys, I really appreciate it!
Cheers!
Fernando
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:56 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|