-
Notifications
You must be signed in to change notification settings - Fork 34
/
keep_this_formats.pl
executable file
·53 lines (42 loc) · 1.04 KB
/
keep_this_formats.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 12 September 2012
# Edit: 11 August 2017
# https://github.com/trizen
# Keep only one or more type of file formats in a directory and its sub-directories.
# List and remove the other formats (when -r is specified).
use 5.010;
use strict;
use warnings;
use File::Find qw(find);
use Getopt::Std qw(getopts);
sub usage {
die <<"USAGE";
usage: $0 [options] <dirs>
options:
-f <formats> : the list of formats (comma separated)
-r : remove the other formats (default: off)
example: $0 -f 'mp3,ogg,wma' /home/Music
USAGE
}
my %opts;
getopts('f:r', \%opts);
$opts{f} // usage();
@ARGV || usage();
my $formats_re = do {
local $" = '|';
my @a = map { quotemeta } split(/\s*,\s*/, $opts{f});
qr/\.(?:@a)\z/i;
};
find {
wanted => sub {
if (not /$formats_re/ and -f) {
say $_;
if ($opts{r}) {
unlink($_) or warn "Can't remove file '$_': $!";
}
}
},
no_chdir => 1,
} => @ARGV;