A utility to mark and operate on files in the shell.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

64 lines
1.2 KiB

#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.0;
use App::MarkFiles qw(each_path remove check_collisions);
use File::Basename;
use File::Copy;
use File::Spec;
use Getopt::Long;
use Pod::Usage;
my $dry_run;
GetOptions(
'no-action|dry-run|n' => \$dry_run,
help => sub { pod2usage(0) },
) or pod2usage(2);
my (@collisions) = check_collisions();
if (scalar @collisions) {
# We got something. Alert the user and bail.
say "Multiple marked paths would move to the following filenames:";
say join "\n", @collisions;
say "";
say "No action taken, since this probably isn't what you want.";
exit(1);
}
my @unmark;
each_path(sub {
my ($path) = @_;
unless (-e $path) {
say "No such file: " . $path;
return;
}
my ($source_basename, $source_path) = fileparse($path);
my $target = File::Spec->catfile('.', $source_basename);
if (-e $target) {
say "Warning: $path will overwrite $target";
}
if ($dry_run) {
say "Would move: $path";
push @unmark, $path;
return;
}
if (move($path, $target)) {
say "Moved: $path";
push @unmark, $path;
} else {
say "Move failed: $!"
}
});
if ($dry_run) {
say "Would remove marks from: " . join ', ', @unmark;
} else {
remove(@unmark);
}