- remove only option added
- some code cleanups
This commit is contained in:
parent
647ea56704
commit
30017b489b
1 changed files with 39 additions and 28 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/perl -w
|
||||
|
||||
# $Dwarf: safe_mv,v 1.3 2002/10/24 11:06:06 ward Exp $
|
||||
# $Dwarf: safe_mv,v 1.4 2002/10/26 17:18:46 ward Exp $
|
||||
# $Source$
|
||||
|
||||
#
|
||||
|
|
@ -8,9 +8,8 @@
|
|||
# op de plek waar het naartoe gemoved wordt eerst even te vergelijken
|
||||
# of ze identiek zijn. Zo nee, gaat de move niet door. Zo ja, delete
|
||||
# te moven bestand.
|
||||
# (C) 2001 Ward Wouts
|
||||
#
|
||||
# Argumenten $source-bestanden $target-dir
|
||||
# (C) 2001-2002 Ward Wouts
|
||||
#
|
||||
|
||||
use File::Copy;
|
||||
|
|
@ -19,28 +18,24 @@ use Getopt::Long;
|
|||
|
||||
$DEBUG = 0;
|
||||
|
||||
if ($#ARGV < 1) { &help; }
|
||||
&cmdline;
|
||||
&process;
|
||||
|
||||
$targetdir=pop;
|
||||
if ( ! -d $targetdir ) { &help; }
|
||||
if ( $DEBUG ) { print "$targetdir\n"; }
|
||||
$opt_r = 1; # just get rid of that stupid message from -w
|
||||
|
||||
foreach ( @ARGV ) {
|
||||
sub process {
|
||||
foreach ( @sources ) {
|
||||
$source = "$_";
|
||||
$target = "$_";
|
||||
$target =~ s/.*\///;
|
||||
$target = $targetdir."/".$target;
|
||||
if ( -e "$target" ) {
|
||||
&compare($source, $target);
|
||||
} else {
|
||||
} elsif ( ! $opt_r ) {
|
||||
if ( $DEBUG ) { print "mv $_ -> $target\n"; }
|
||||
else { move("$_", "$target") or die "move failed: $!"; }
|
||||
}
|
||||
}
|
||||
|
||||
sub help {
|
||||
print "Usage: $0 source_files target_dir\n";
|
||||
exit 1;
|
||||
}
|
||||
}
|
||||
|
||||
sub compare($source, $target) {
|
||||
|
|
@ -55,11 +50,11 @@ sub compare($source, $target) {
|
|||
( $source_stat[0] == $target_stat[0] )) {
|
||||
print "Skipped $source & $target are one and the same!\n"
|
||||
} else {
|
||||
if ( $DEBUG ) { print "$source and $target are the same size\n"; }
|
||||
print "$source and $target are the same size\n" if $DEBUG;
|
||||
$source_digest = &calc_md5($source);
|
||||
if ( $DEBUG ) { print "source digest $source_digest\n"; }
|
||||
print "source digest $source_digest\n" if $DEBUG;
|
||||
$target_digest = &calc_md5($target);
|
||||
if ( $DEBUG ) { print "source digest $target_digest\n"; }
|
||||
print "source digest $target_digest\n" if $DEBUG;
|
||||
if ( $source_digest eq $target_digest ) {
|
||||
print "Files are the same. Deleting $source.\n";
|
||||
unlink($source)
|
||||
|
|
@ -87,12 +82,28 @@ sub calc_md5($file) {
|
|||
}
|
||||
|
||||
sub cmdline {
|
||||
&GetOptions("h", "r");
|
||||
|
||||
&help if $opt_h;
|
||||
|
||||
if ($#ARGV < 1) { &help; }
|
||||
|
||||
$targetdir=pop(@ARGV);
|
||||
if ( ! -d $targetdir ) { &help; }
|
||||
if ( $DEBUG ) { print "$targetdir\n"; }
|
||||
|
||||
@sources = @ARGV;
|
||||
}
|
||||
|
||||
sub help {
|
||||
print <<EOT;
|
||||
$opt_h = 1; # just get rid of that stupid message from -w
|
||||
print << "EOT";
|
||||
|
||||
$0 [options] <files> <destination>
|
||||
|
||||
-h this help message
|
||||
-r remove duplicates only, don't move files
|
||||
|
||||
EOT
|
||||
exit 1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue