- remove only option added

- some code cleanups
This commit is contained in:
Ward Wouts 2002-10-26 20:20:00 +00:00
parent 647ea56704
commit 30017b489b

View file

@ -1,16 +1,15 @@
#!/usr/bin/perl -w #!/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$ # $Source$
# #
# Scriptje om bestanden te moven, en bij het reeds bestaan van een bestand # Scriptje om bestanden te moven, en bij het reeds bestaan van een bestand
# op de plek waar het naartoe gemoved wordt eerst even te vergelijken # 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 # of ze identiek zijn. Zo nee, gaat de move niet door. Zo ja, delete
# te moven bestand. # te moven bestand.
# (C) 2001 Ward Wouts
# #
# Argumenten $source-bestanden $target-dir # (C) 2001-2002 Ward Wouts
# #
use File::Copy; use File::Copy;
@ -19,30 +18,26 @@ use Getopt::Long;
$DEBUG = 0; $DEBUG = 0;
if ($#ARGV < 1) { &help; } &cmdline;
&process;
$targetdir=pop; $opt_r = 1; # just get rid of that stupid message from -w
if ( ! -d $targetdir ) { &help; }
if ( $DEBUG ) { print "$targetdir\n"; }
foreach ( @ARGV ) { sub process {
$source = "$_"; foreach ( @sources ) {
$target = "$_"; $source = "$_";
$target =~ s/.*\///; $target = "$_";
$target = $targetdir."/".$target; $target =~ s/.*\///;
if ( -e "$target" ) { $target = $targetdir."/".$target;
&compare($source, $target); if ( -e "$target" ) {
} else { &compare($source, $target);
if ( $DEBUG ) { print "mv $_ -> $target\n"; } } elsif ( ! $opt_r ) {
else { move("$_", "$target") or die "move failed: $!"; } 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) { sub compare($source, $target) {
my ($source_size, $target_size); my ($source_size, $target_size);
my (@source_stat, @target_stat); my (@source_stat, @target_stat);
@ -55,14 +50,14 @@ sub compare($source, $target) {
( $source_stat[0] == $target_stat[0] )) { ( $source_stat[0] == $target_stat[0] )) {
print "Skipped $source & $target are one and the same!\n" print "Skipped $source & $target are one and the same!\n"
} else { } 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); $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); $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 ) { if ( $source_digest eq $target_digest ) {
print "Files are the same. Deleting $source.\n"; print "Files are the same. Deleting $source.\n";
unlink($source) unlink($source)
or die "Can't delete $source: $!\n"; or die "Can't delete $source: $!\n";
} else { } else {
print "Skipped mv $source -> $target files differ\n" print "Skipped mv $source -> $target files differ\n"
@ -87,12 +82,28 @@ sub calc_md5($file) {
} }
sub cmdline { 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 { sub help {
print <<EOT; $opt_h = 1; # just get rid of that stupid message from -w
print << "EOT";
$0 [options] <files> <destination> $0 [options] <files> <destination>
-h this help message
-r remove duplicates only, don't move files
EOT EOT
exit 1;
} }