How to compare files in 2 dirs and write results on a file
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.