LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-30-2009, 07:47 AM   #1
hadimotamedi
Member
 
Registered: Aug 2009
Posts: 228

Rep: Reputation: 30
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
 
Old 11-30-2009, 07:55 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 11-30-2009, 07:58 AM   #3
kea_kea
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Rep: Reputation: 0
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.
 
Old 12-02-2009, 01:55 AM   #4
hadimotamedi
Member
 
Registered: Aug 2009
Posts: 228

Original Poster
Rep: Reputation: 30
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 View Post
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.
 
Old 12-02-2009, 03:56 AM   #5
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
1 members found this post helpful.
Old 12-02-2009, 04:38 AM   #6
hadimotamedi
Member
 
Registered: Aug 2009
Posts: 228

Original Poster
Rep: Reputation: 30
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 View Post
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.
 
Old 12-02-2009, 05:29 AM   #7
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
Old 12-02-2009, 06:26 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by hadimotamedi View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
can someone compare this 2 folders ..HELP bladez99 Linux - Newbie 4 08-10-2009 01:18 PM
compare folder contents babysnake Linux - Newbie 4 02-09-2007 08:04 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
How to compare two folders contents? rvoigt Linux - Newbie 6 01-07-2005 01:35 AM
to compare the contents of 2 files MaleWithBrains Linux - Newbie 3 01-27-2004 07:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration