Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # recursively choose a number of files optionally matching a regex
- # from a directory or list of directories. defaults to cwd.
- # outputs absolute paths by default, relative with -r
- # use like so:
- # randomfile -n 23 -p \(mp3\|ogg\|flac\)$ ~/music ~/music2
- use strict;
- use warnings;
- use Getopt::Long;
- use Cwd;
- use File::Random qw/random_file/;
- use File::Spec;
- my $pat = '^.+$';
- my $num = 1;
- my $relative = 0;
- GetOptions(
- 'p|pattern=s' => \$pat,
- 'n|number=i' => \$num,
- 'r|relative' => \$relative,
- );
- while ($num) {
- my $random_file;
- my $dir;
- do {
- $dir = $ARGV[rand @ARGV] or cwd();
- $random_file = random_file( -dir => $dir,
- -check => qr/$pat/,
- -recursive => 1);
- } until $random_file;
- $random_file = File::Spec->catfile($dir, $random_file);
- $random_file = File::Spec->abs2rel($random_file) if $relative;
- print "$random_file\n";
- --$num;
- }
Add Comment
Please, Sign In to add comment