LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-18-2010, 09:32 AM   #1
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Rep: Reputation: 0
Exclamation Comparing all the files in a directory, by content.


I wish to compare all the files in a directory....comparing is by content. And same files should be printed....

echo "program : $0"

cd $1

for a in *
do
for b in *
do
diff -as $a $b > /dev/null
if [ $? -eq 0 ];
then
echo "$a and $b in $1 same..."
fi
done
done

This logic did not give the correct result....please help...!!!
 
Old 09-18-2010, 11:10 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,662

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by apanimesh061 View Post
I wish to compare all the files in a directory....comparing is by content. And same files should be printed....
Code:
echo "program : $0"
cd $1
for a in *
do
  for b in *
    do  
       diff -as $a $b > /dev/null
         if [ $? -eq 0 ];
            then
              echo "$a and $b in $1 same..."
         fi
    done
done
This logic did not give the correct result....please help...!!!
Please put your code in CODE tags. And have you checked your other thread, where you ask the same question, and have gotten replies?
 
Old 09-19-2010, 04:10 AM   #3
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
Exclamation

NO...NO....
In previous thread I was comparing files in two different directories.....

In the second thread I wish to compare all the files within the a single directory.....eg

Directory---d1
contains files f1, f2, f3, ....fn....I have to print which files have same content....
 
Old 09-19-2010, 05:10 AM   #4
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Try fdupes.
 
1 members found this post helpful.
Old 09-19-2010, 05:57 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
#!/usr/bin/env ruby -w
require 'digest'
def getdigest(file)
   Digest::MD5.hexdigest(File.open(file, "rb") { |f| f.read })
end

hash = Hash.new { |h,k| h[k]="" }
Dir["*"].each do
  |x|
  File.file?(x) and hash[x]=getdigest(x)
end
u=hash.values.uniq
v=hash.values
u.each do
  |z|
  col=[] #to collect identical
  if v.count(z) > 1
      hash.each{|x,y| col<<x if y==z}
  end
  print  "These files are identical: #{col.join(" ")}\n" if !col.empty?
end
 
Old 09-19-2010, 10:04 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,662

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by apanimesh061 View Post
NO...NO....
In previous thread I was comparing files in two different directories.....

In the second thread I wish to compare all the files within the a single directory.....eg

Directory---d1
contains files f1, f2, f3, ....fn....I have to print which files have same content....
Ok, so why can't you modify that code to read from the same directory?? And this sounds very much like a homework question...
 
  


Reply



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
comparing files newbiesforever Linux - Software 3 07-07-2010 03:20 PM
help: regarding comparing two directory tree structures raj_thota Linux - Software 2 11-02-2005 12:19 PM
hel: regarding comparing two directory tree structures raj_thota Programming 1 11-02-2005 07:16 AM
Help: regarding comparing two directory tree structures raj_thota Solaris / OpenSolaris 1 11-02-2005 07:16 AM
Comparing 2 Files xianzai Programming 2 05-23-2004 11:50 AM

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

All times are GMT -5. The time now is 11:53 AM.

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
Open Source Consulting | Domain Registration