Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for duplicate files 1

Status
Not open for further replies.

rossco100

Programmer
Mar 4, 2006
5
GB
Hi, im trying to write a script that searches for identical file names and file sizes , i know that this can be done using the -c option but i am not quite sure how to start it, any help would be great thanks.
 
Im trying to write a script that searches a filesystem to find all duplicate files and then provides an output.
Any help greatly appreciated.

Thanks
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
ruby common-files.rb dir1 dir2
common-files.rb:
Code:
##  Find files common to two directories.
##  Descend into subdirectories.
lists = [ Hash.new { [] }, Hash.new { [] } ]
for i in 0 .. 1
  Dir[ ARGV[i] + "/**/*" ].each{|path|
    lists[i][
      [ File.basename(path), File.size(path) ]
    ] += [ path ]  if File.file?( path )
  }
end

lists.first.each_key { |key|
  if lists.last.include?( key )
    puts "---  #{ key[0,2].join('  ') } bytes"
    puts lists.first[ key ], lists.last[ key ]
  end 
}
 
Try this:

Code:
find . -type f -exec sum {} \;|\
 sort|\
 awk '{
 if (lsum == $1) {
  if (lfile != "" ) {
   print lfile
   lfile=""
   blank="yes"
  }
  print $3
 }
 else {
  if (blank == "yes") {
   print ""
  }
  lsum=$1
  lfile=$3
 }
}'

HTH,

p5wizard
 
You can check for the identical number of blocks in addition to the checksum value but I don't think it's absolutely necessary:

Code:
find . -type f -exec sum {} \;|\
 sort|\
 awk '{
 if ((lsum == $1) && (lblks == $2)){
  if (lfile != "" ) {
   print lfile
   lfile=""
   blank="yes"
  }
  print $3
 }
 else {
  if (blank == "yes") {
   print ""
  }
  lsum=$1
  lblks=$2
  lfile=$3
 }
}'

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top