Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # recursively choose a number of files optionally matching a regex
  4. # from a directory or list of directories. defaults to cwd.
  5. # outputs absolute paths by default, relative with -r
  6. # use like so:
  7. # randomfile -n 23 -p \(mp3\|ogg\|flac\)$ ~/music ~/music2
  8.  
  9. use strict;
  10. use warnings;
  11. use Getopt::Long;
  12. use Cwd;
  13. use File::Random qw/random_file/;
  14. use File::Spec;
  15.  
  16. my $pat = '^.+$';
  17. my $num = 1;
  18. my $relative = 0;
  19.  
  20. GetOptions(
  21. 'p|pattern=s' => \$pat,
  22. 'n|number=i' => \$num,
  23. 'r|relative' => \$relative,
  24. );
  25.  
  26. while ($num) {
  27. my $random_file;
  28. my $dir;
  29. do {
  30. $dir = $ARGV[rand @ARGV] or cwd();
  31. $random_file = random_file( -dir => $dir,
  32. -check => qr/$pat/,
  33. -recursive => 1);
  34. } until $random_file;
  35. $random_file = File::Spec->catfile($dir, $random_file);
  36. $random_file = File::Spec->abs2rel($random_file) if $relative;
  37. print "$random_file\n";
  38. --$num;
  39. }
Add Comment
Please, Sign In to add comment