From eb9135dded650fcc0c87417fc7b44dbb6995f8f2 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Thu, 30 Aug 2001 08:36:08 +0000 Subject: [PATCH] ook maar es in cvs kwakken. kan ik um een keer fatsoenlijk maken... --- list_same/list_same | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 list_same/list_same diff --git a/list_same/list_same b/list_same/list_same new file mode 100755 index 0000000..8aafbab --- /dev/null +++ b/list_same/list_same @@ -0,0 +1,69 @@ +#!/usr/bin/perl -w + +# Generates a list of files in cur. dir which are actually the same +# Compares size and MD5 checksum +# Handy for cleaning up pictures like this: +# list_same|cut -f 1 -d " "|xargs rm + +# This still sucks big time! +# Can it handle directories at all? +# Why does it bother with calculating md5 checksums of everything, +# instead of just the files that have the same size? + +# Changelog: +# 2001-08-29: +# use MD5; instead of a shell call to md5 +# some cleaning of code + +use MD5; + +opendir(DIR, ".") or die "can't open . $!"; +while (defined($file = readdir(DIR))) { + &get_finfo; +} +closedir(DIR); + +$old_md5=""; +$old_size=""; +$old_name=""; + +foreach $line (sort(@file_lijstje)) { +# print $line; + $line .= "\n"; + $line =~ /(.*?)\s(.*?)\s(.*)/; + $md5 = $1; $size = $2; $name = $3; + if (($old_md5 eq $md5) && ($old_size eq $size)) { + print "$old_name $name\n"; + } else { + $old_md5=$md5; + $old_size=$size; + $old_name=$name; + } +} + +################################# +# dain bread functions + +sub get_finfo() { + my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks); + my $line; + if (($file ne ".")&&($file ne "..")) { + ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, + $atime,$mtime,$ctime,$blksize,$blocks) = stat $file; + $line = &calc_md5($file); + push @file_lijstje, "$line $size $file"; + } +} + +sub calc_md5($file) { + my ($file, $digest); + $file = shift; + $md5 = new MD5; + open FILE, "<$file" or die "couldn't open file: $!\n"; + seek(FILE, 0, 0); + $md5->reset; + $md5->addfile(FILE); + $digest = $md5->hexdigest; + close(FILE); + return $digest; +}