home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1639 < prev    next >
Internet Message Format  |  1990-12-28  |  2KB

  1. From: merlyn@iwarp.intel.com (Randal Schwartz)
  2. Newsgroups: alt.sources
  3. Subject: Re: cmpall -- find identical (duplicated) files
  4. Message-ID: <1990Aug3.043422.16970@iwarp.intel.com>
  5. Date: 3 Aug 90 04:34:22 GMT
  6.  
  7. In article <1990Aug2.212411.3078@sq.sq.com>, lee@sqarc (Liam R. E. Quin) writes:
  8. | I wrote cmpall some time ago, when I found that I had lots of copies and
  9. | duplicated directory hierarchies.
  10.  
  11. "That's not a knife... *This* (pulls out his piece) is a knife!"
  12.  
  13. Here's a little ditty called "findsame".  It first does what you do,
  14. in that it finds files that are the same length.  For all files of the
  15. same length, it then runs "sum" to see who's actually the same, and
  16. then "ls -l"'s the matching pairs combinations.
  17.  
  18. Perl, of course.
  19.  
  20. ================================================== cut here
  21. #!/local/usr/bin/perl
  22.  
  23. $| = 1;
  24.  
  25. @ARGV = ('.') unless $#ARGV >= 0;
  26.  
  27. open(F,"find @ARGV -type f -print|") || die "Cannot open find ($!)";
  28. while (<F>) {
  29.     chop;
  30.     @stat = stat($_);
  31.     $bysize{$stat[7]} .= "$_\n";
  32. }
  33. close(F);
  34.  
  35. sub numeric {
  36.     0+$a < 0+$b ? -1 : 1;
  37. }
  38.  
  39. for $asize (sort numeric keys(bysize)) {
  40.     @files = split(/\n/, $bysize{$asize});
  41.     next if $#files <= 0;
  42.     unless(open(S,"-|")) {
  43.         exec "sum", @files;
  44.     }
  45.     %bysum = ();
  46.     while (<S>) {
  47.         chop;
  48.         @F = split;
  49.         $bysum{$F[0]} .= "$F[2]\n";
  50.     }
  51.     close(S);
  52.     for $asum (sort numeric keys(bysum)) {
  53.         @files = split(/\n/, $bysum{$asum});
  54.         next if $#files <= 0;
  55.         system 'ls','-li', @files;
  56.         print "\n"; # to separate them by blank lines
  57.     }
  58. }
  59. ==================================================
  60.  
  61. It's not the best Perl code (I wrote it a long time ago).  I'd
  62. probably rewrite it quite a bit now. :-)
  63.  
  64. Just another Perl hacker,
  65. -- 
  66. /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
  67. | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
  68. | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
  69. \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/
  70.