*joy*, now list_same understands multiple directories as arguments. It

will list the same files in multiple directories, and when you give the
-s optioen, it will prefer to not show the first match in the first
directory... This makes it very easy to delete newly downloaded files
that you already have. ;)
This commit is contained in:
Ward Wouts 2003-02-06 22:11:13 +00:00
parent 84fca7e6ae
commit 6b6935761a

View file

@ -17,9 +17,9 @@ use Digest::MD5;
use Getopt::Std; use Getopt::Std;
my %opts = (); my %opts = ();
getopts('ehms', \%opts); my @dirs;
if ( $opts{h} ) { &help; } &cmdline;
my @filelist = &getdir; my @filelist = &getdir;
if ( $opts{m} ) { if ( $opts{m} ) {
@ -32,15 +32,27 @@ if ( $opts{m} ) {
################################# #################################
# functions # functions
sub cmdline {
getopts('ehms', \%opts);
if ( $opts{h} ) { &help; }
map { if ( -d $_ ) { s/\/$//; push @dirs, $_; } else { die "$_ not a directory" } } @ARGV;
if ( scalar @dirs == 0 ) {
push @dirs, ".";
}
}
sub getdir { sub getdir {
my ( $file, @filelist ); my ( $file, @filelist );
opendir(DIR, ".") or die "can't open . $!"; foreach ( @dirs ) {
while (defined($file = readdir(DIR))) { opendir(DIR, $_) or die "can't open $_: $!";
if ( -f $file ) { while (defined($file = readdir(DIR))) {
push @filelist, $file; if ( -f "$_/$file" ) {
push @filelist, "$_/$file";
}
} }
closedir(DIR);
} }
closedir(DIR);
return @filelist; return @filelist;
} }
@ -156,7 +168,7 @@ sub output_doubles(@) {
foreach $key (keys %md5s) { foreach $key (keys %md5s) {
if ( @{$md5s{$key}} > 1 ) { if ( @{$md5s{$key}} > 1 ) {
@files = sort @{$md5s{$key}}; @files = sort dirsort @{$md5s{$key}};
for $i ($start .. $#files) { for $i ($start .. $#files) {
chomp $files[$i]; chomp $files[$i];
&output("$files[$i]\n"); &output("$files[$i]\n");
@ -175,6 +187,34 @@ sub output($) {
print "$string"; print "$string";
} }
sub dirsort {
my $firstdir = $dirs[0];
if ( $a =~ /^$firstdir\/[^\/]*$/ && $b =~ /^$firstdir\/[^\/]*$/ ) {
return &wardsort;
} elsif ( ( $a =~ /^$firstdir\/[^\/]*$/ ) && ! ( $b =~ /^$firstdir\/[^\/]*$/ ) ) {
return -1;
} elsif ( ! ( $a =~ /^$firstdir\/[^\/]*$/ ) && ( $b =~ /^$firstdir\/[^\/]*$/ ) ) {
return 1;
} else {
return &wardsort;
}
}
sub wardsort {
my @a = split(/([0-9]+)/,$a);
my @b = split(/([0-9]+)/,$b);
foreach my $x (@a)
{
my $y = shift @b;
my $r = ($x =~ /^[0-9]+$/ && $y =~ /^[0-9]+$/) ?
($x <=> $y) : ($x cmp $y);
$r && return $r;
}
return -1 if (@b);
0;
}
sub help { sub help {
my $name = $0; my $name = $0;
$name =~ s/.*\///; $name =~ s/.*\///;