Hello Monks, Looking for some efficient solutions in printing the duplicate files within a directory

Here is my code.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ############## my $dir = "$ARGV[0]"; my %md5sum; my @md5; my $flag = 0; my %seen; opendir(my $dh, $dir) || die "Unable to Open the Directory: $!\n"; chdir $dir or die "Cannot Change directory: $!\n"; while (my $file = readdir $dh) { chomp $file; next if $file =~ /^\.+$/g; if (-f $file) { my ($md) = (split /\s+/, qx(/usr/bin/md5sum $file))[0]; $md5sum{$file} = $md; push @md5, $md; } } closedir($dh); my @uniq = grep { $seen{$_}++ } @md5; foreach my $k (keys %md5sum) { foreach my $md (@uniq) { if ($md eq $md5sum{$k}) { $flag = 1; last; } } if ($flag) { print "$k is a duplicate file with MD5 of $md5sum{$k}\n"; $flag = 0; }else { print "$k is not a duplicate file, It's md5sum is $md5sum{$k}\n" +; } }
-bash-3.2$ ./duplicate_files.pl /users/scripts/perl/test/ file2 is a duplicate file with MD5 of d41d8cd98f00b204e9800998ecf8427e file1 is a duplicate file with MD5 of 5bb062356cddb5d2c0ef41eb2660cb06 file3 is a duplicate file with MD5 of d41d8cd98f00b204e9800998ecf8427e file4 is a duplicate file with MD5 of d41d8cd98f00b204e9800998ecf8427e file5 is a duplicate file with MD5 of 5bb062356cddb5d2c0ef41eb2660cb06 file6 is not a duplicate file, It's md5sum is d617c2deabd27ff86ca9825b +2e7578d4

In reply to List Duplicate Files in a given directory by pr33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.