in reply to Recursive search for duplicate files

Well file and folder names are sufficient.
use warnings; use strict; use File::Find; my $directory = '/mnt/music/very-good'; find (\&wanted, $directory); my %path_file; sub wanted { my $path = $File::Find::dir; my $filename = $File::Find::name; $path_file{$path} = $filename; my %count; while (my ($key , $value) = each(%path_file)) { $count{$key} +=1; } }

Replies are listed 'Best First'.
Re^2: Recursive search for duplicate files
by moritz (Cardinal) on Nov 27, 2007 at 14:16 UTC
    There you go: the idea is to store for each filename in which paths it occurs.
    #!/usr/bin/perl use warnings; use strict; use File::Find; use File::Spec; my $directory = shift @ARGV || '/mnt/music/very-good'; find (\&wanted, $directory); my %path_file; sub wanted { my $path = $File::Find::dir; my $filename = File::Spec->abs2rel($File::Find::name, $path); push @{$path_file{$filename}}, $path; } while (my ($filename, $paths) = each %path_file){ if (scalar @$paths >= 2){ print "$filename occurs in these paths: ", join(", ", @$paths) +, "\n"; } # else { # print "$filename is uniq\n"; # } }
      I have a question: Since %path_file is a hash why becomes an array here:
      push @{$path_file{$filename}}, $path;
      Dereferencing the array @path_file or the hash %path_file
        The hash's values are references to arrays.

        Since push expects an array, not an array ref, it needs to be dereferenced with the @{ $array_ref } syntax.