Over a million developers have joined DZone.

Thin-copy

·
#
# Remove files that have been successfully copied to another location.
#
# I use it something like this:
#


$ cp -rp orig_dir copy_dir
$ cd orig_dir
$ find * -type f -exec md5sum {} \; > orig
$ cd ../copy_dir
$ find * -type f -exec md5sum {} \;  > ../orig_dir/copy
$ cd ../orig_dir
$ thin-copy


#
# The code...
#


#!/usr/bin/perl
#
# thin-copy - 31/12/03
# http://snippets.dzone.com/posts/show/6950
#

use warnings;
use strict;

sub DEBUG {0}

my (%copy_sums, @orig_sums, %orig_count, %orig_files, %uniq_orig);
my $debug = 0;

open (COPY, "copy") || open (COPY, "copy.md5") || die "Can't read 'copy': $!\n";
while() {
        if (/^([a-f\d]{32})\s+(.*)$/) {
                $copy_sums{$1}++;
        }
}
close(COPY);

open (ORIG, "orig") || die "Can't read 'orig': $!\n";
while() {
        if (/^([a-f\d]{32})\s+(.*)$/) {
                $orig_files{$1}{++$orig_count{$1}} = $2;
                push(@orig_sums,$1) unless($uniq_orig{$1}++);
        }
}
close(ORIG);

for my $sum (@orig_sums) {

        DEBUG && printf "$sum\n";

        if ($copy_sums{$sum}) {
                for my $count (1 .. $orig_count{$sum}) {
                        my $this_file = $orig_files{$sum}{$count};
                        if ( -f $this_file ) {
                                print "rm: $this_file\n";
                                unlink($this_file);
                        } else {
                                print "File not found: $this_file\n";
                        }
                }
        } else {
                for my $count (1 .. $orig_count{$sum}) {
                        print "No copy for: $orig_files{$sum}{$count}\n";
                }
        }
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}