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

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