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.

55 lines
1.1 KiB

  1. #!/usr/bin/env perl
  2. use warnings;
  3. use strict;
  4. use 5.10.0;
  5. use App::MarkFiles qw(each_path check_collisions);
  6. use File::Basename;
  7. use File::Copy;
  8. use File::Spec;
  9. use Getopt::Long;
  10. my $dry_run;
  11. GetOptions(
  12. 'no-action|dry-run|n' => \$dry_run,
  13. help => sub { pod2usage(0) },
  14. ) or pod2usage(2);
  15. my (@collisions) = check_collisions();
  16. if (scalar @collisions) {
  17. # We got something. Alert the user and bail.
  18. say "Multiple marked paths would be copied to the following filenames:";
  19. say join "\n", @collisions;
  20. say "";
  21. say "No action taken, since this probably isn't what you want.";
  22. exit(1);
  23. }
  24. each_path(sub {
  25. my ($path) = @_;
  26. unless (-e $path) {
  27. say "No such file: $path";
  28. return;
  29. }
  30. my ($source_basename, $source_path) = fileparse($path);
  31. my $target = File::Spec->catfile('.', $source_basename);
  32. if (-e $target) {
  33. say "Warning: $path will overwrite $target";
  34. # See marks-mv for some discussion of what happens if target exists.
  35. }
  36. if ($dry_run) {
  37. say "Would copy: $path";
  38. return;
  39. }
  40. if (copy($path, './')) {
  41. say "Copied: $path";
  42. } else {
  43. say "Copy failed: $!"
  44. }
  45. });