App-size-dir-diff
view release on metacpan or search on metacpan
script/size-dir-diff view on Meta::CPAN
sub search_first {
if (-f $_) {
my $key = $File::Find::name =~ s/^$ARGV[0]//r;
$first_hash{ $key } = -s $_;
}
}
sub search_second {
if (-f $_) {
my $key = $File::Find::name =~ s/^$ARGV[1]//r;
$second_hash{ $key } = -s $_;
}
}
sub print_hashes {
print("Files in $ARGV[1] that differ from files in $ARGV[0]:\n");
foreach my $key (keys %second_hash) {
if (exists($first_hash{ $key }) && ($first_hash{ $key } != $second_hash{ $key })) {
print "$key $first_hash{ $key } != $second_hash{ $key }\n";
print "Delete $ARGV[1]/$key? (Y/N)";
chomp(my $answer = <STDIN>);
if (uc($answer) eq 'Y') {
system("rm \"$ARGV[1]/$key\"");
}
}
}
if ($opts{ 'd' }) {
print "\nFiles in $ARGV[1] that have a duplicate in $ARGV[0]:\n";
foreach my $key (keys %second_hash) {
if (exists($first_hash{ $key }) && ($first_hash{ $key } == $second_hash{ $key })) {
if ($opts{ 'e' }) {
print "Delete $ARGV[1]/$key? (Y/N)";
chomp(my $answer = <STDIN>);
if (uc($answer) ne 'N') {
system("rm \"$ARGV[1]/$key\"");
}
}
else {
print "$key\n";
}
}
}
}
if ($opts{ 'n' }) {
print "\nFiles in $ARGV[1] that do not exist in $ARGV[0]:\n";
foreach my $key (keys %second_hash) {
if (not exists($first_hash{ $key })) {
print "$key\n";
}
}
}
}
sub print_help {
print "\nsize-dir-diff - find differences between two directories\n\n";
print "Usage: size-dir-diff [OPTION] PATH1 PATH2\n\n";
print "Options:\n";
print "\t-d\tprint duplicates\n";
print "\t-e\tprompt if duplicates should be deleted (in PATH2)\n";
print "\t-n\tprint files in PATH2 that do not exist in PATH1\n";
print "\t-h\tprints this help\n";
exit 0;
}
getopts('dhen', \%opts);
if ($#ARGV ne 1 or $opts{ 'h' }) {
print_help();
}
print "Comparing [2] '$ARGV[1]' to [1] '$ARGV[0]' ..";
if (-f $file) {
%first_hash = %{ retrieve($file) };
}
else {
find(\&search_first, $ARGV[0]);
store(\%first_hash, $file);
}
find(\&search_second, $ARGV[1]);
printf("\t\t\t(file count: [2]: %s [1]: %s)\n", scalar keys %second_hash, scalar keys %first_hash);
print_hashes();
print "\n";
=head1 NAME
size-dir-diff - find differences between two directories
=head1 VERSION
version 0.4.1
=head1 DESCRIPTION
A script that finds differences between two directories.
Two directories are compared taking the size of each file.
Files in the second argument are treated as a subset of the first.
Prompts user if differing files should be deleted.
To speed up comparisons, size-dir-diff will always try to store a copy of the
first hash (PATH1) in /tmp/first_hash. Keep this in mind since a subsequent call
will read /tmp/first_hash into memory. This requires that /tmp/first_hash must
first be deleted, before comparing a new PATH1.
=head1 SYNOPSIS
$ size_dir_diff -d /usr/src/BPi/device-tree /usr/src/BPi/device-tree.copy
Files in /usr/src/BPi/device-tree.copy that differ from files in /usr/src/BPi/device-tree:
Files in /usr/src/BPi/device-tree.copy that have a duplicate in /usr/src/BPi/device-tree:
/sun7i-a20-bananapi.dtb
=head1 AUTHOR
Jonas Jensen
=head1 LICENSE
GPL_2
( run in 1.195 second using v1.01-cache-2.11-cpan-6aa56a78535 )