Inquiry:How to compare the contents of two folders?
Linux - NewbieThis 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
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.
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
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.
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
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.
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.
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
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.
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 difffirst_filesecond_file
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.