-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathplocate.pl
executable file
·67 lines (49 loc) · 1.21 KB
/
plocate.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl
# Author: Trizen
# Date: 20 April 2012
# https://github.com/trizen
# Perl locate - a pretty efficient file locater
use 5.010;
use strict;
use Getopt::Std qw(getopts);
use File::Find qw(find);
use File::Spec::Functions qw(rel2abs);
my $DB_FILE = rel2abs('plocate.db');
sub usage {
print <<"HELP";
usage: $0 [options] [dirs]
options:
-g : generate a $DB_FILE file
-i : insensitive match
-h : show this message
example: $0 -g /my/dir
$0 /tmp/(work|shop).doc
HELP
exit 0;
}
@ARGV or do { warn "$0: no pattern to search for specified\n"; exit 1 };
my %opt;
getopts('gih', \%opt);
$opt{h} && usage();
if ($opt{g}) {
open my $DB_FH, '>', $DB_FILE or die "$0: Can't open $DB_FILE: $!";
say {$DB_FH} q{<<'__END_OF_THE_DATABASE__';};
find {
no_chdir => 1,
wanted => sub {
say {$DB_FH} rel2abs($_);
},
} => @ARGV ? grep { -d } @ARGV : q{.};
say {$DB_FH} q{__END_OF_THE_DATABASE__};
close $DB_FH;
exit 0;
}
-e $DB_FILE or usage();
my $files = do $DB_FILE;
study $files;
foreach my $re (@ARGV) {
$re = $opt{i} ? qr{$re}i : qr{$re};
while ($files =~ /^.*?$re.*/gmp) {
say ${^MATCH};
}
}