LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Inquiry:How to compare the contents of two folders? (https://www.linuxquestions.org/questions/linux-newbie-8/inquiry-how-to-compare-the-contents-of-two-folders-772416/)

hadimotamedi 11-30-2009 06:47 AM

Inquiry:How to compare the contents of two folders?
 
Dear All
Please be informed that on my RedHat 7.2 server I have two conf folders . I want to compare their contents to see which files are different in size & content . Can you please do me favor and let me know how can I accomplish this ?
Let me thank you in advance

zhjim 11-30-2009 06:55 AM

The all mighty diff should get you going.
It has a recursive option, an option that tells you if things just differ (normaly it prints out every diffrence), can tell you if files are missing.
Just dig around the man page and see what suits.

Cheers Zhjim

kea_kea 11-30-2009 06:58 AM

Dear Hadimotamedi,

e.g. you can try the following:

ls -l /path/to/conf1_dir > conf1.lst
ls -l /path/to/conf2_dir > conf2.lst
diff conf1.lst conf2.lst

This will show you differences between the contents of the two directories.

If you need to compare two files only to know if they differ (or not), use

md5sum file1
md5sum file2

and see if the checksums are the same or not.

To find the difference _in_ the contents of two files, use diff once again:

diff file1 file2

You can pipe the above diff command if you want:

diff file1 file 2 | grep -e '^< ' -e '^> '

to have only those rows which show the different lines.

etc.

Have a good day,
KEA.

hadimotamedi 12-02-2009 12:55 AM

Thank you for your reply . Please be informed that in my application the two files have similar data in their rows (but not sorted) . I need to find how many rows they have in common (in other words , how many rows with the same data in both of the files) ? As I understand , the diff just compares the rows in one-to-one correspondence and it does not take into account of the similairty between say row#1 in the first file with say row#3 in the second file . Can you please let me know how to modify it to get the desired result ?
Thank you in advance


Quote:

Originally Posted by kea_kea (Post 3774277)
Dear Hadimotamedi,

e.g. you can try the following:

ls -l /path/to/conf1_dir > conf1.lst
ls -l /path/to/conf2_dir > conf2.lst
diff conf1.lst conf2.lst

This will show you differences between the contents of the two directories.

If you need to compare two files only to know if they differ (or not), use

md5sum file1
md5sum file2

and see if the checksums are the same or not.

To find the difference _in_ the contents of two files, use diff once again:

diff file1 file2

You can pipe the above diff command if you want:

diff file1 file 2 | grep -e '^< ' -e '^> '

to have only those rows which show the different lines.

etc.

Have a good day,
KEA.


zhjim 12-02-2009 02:56 AM

Code:

mkdir /tmp/first_dir
mkdir /tmp/second_dir
for i in $(ls first_dir); do
sort $i > /tmp/first_dir/$i
done
for i in $(ls second_dir); do
sort $i > /tmp/second_dir/$i
done
diff -r /tmp/first_dir /tmp/second_dir

This will sort all the files in the two dirs and then compares them with diff.

hadimotamedi 12-02-2009 03:38 AM

Thank you for your help . Please be informed that I tried for your code but it returned just the followings :
"
#diff -r /tmp/first_dir /tmp/second_dir
Only in /tmp/first_dir : Edit3
Only in /tmp/second_dir: Edit4
"
Please be informed that the two files that I want to compare are the Edit3 & Edit4 that I have copied them into the intended folders . Can you please let me know why the output of your code is so unclear ? Actually , I want to compare these two files but not in line-by-line basis .
Thank you in advance


Quote:

Originally Posted by zhjim (Post 3776731)
Code:

mkdir /tmp/first_dir
mkdir /tmp/second_dir
for i in $(ls first_dir); do
sort $i > /tmp/first_dir/$i
done
for i in $(ls second_dir); do
sort $i > /tmp/second_dir/$i
done
diff -r /tmp/first_dir /tmp/second_dir

This will sort all the files in the two dirs and then compares them with diff.


zhjim 12-02-2009 04:29 AM

The -r option to diff lets it recurseivly walk directories. But for this to work the files have to have the same name.

Try this
Code:

diff /tmp/first_dir/Edit3 /tmp/second_dir/Edit4
If you only want to compare two files you don't have to have them in seperate directories.
Import things are.
Have both files sorted with sort and save them to individual files.
Then diff first_file second_file

evo2 12-02-2009 05:26 AM

Quote:

Originally Posted by hadimotamedi (Post 3776780)
Thank you for your help . Please be informed that I tried for your code but it returned just the followings :
"
#diff -r /tmp/first_dir /tmp/second_dir
Only in /tmp/first_dir : Edit3
Only in /tmp/second_dir: Edit4
"
Please be informed that the two files that I want to compare are the Edit3 & Edit4 that I have copied them into the intended folders . Can you please let me know why the output of your code is so unclear ? Actually , I want to compare these two files but not in line-by-line basis .
Thank you in advance

Please be informed that it is because the files have different names. The script is set up assuming that the files you want to compare have the same name.

Evo2.


All times are GMT -5. The time now is 08:09 AM.