我有一个名为Lib的文件夹,我使用File :: Find模块搜索整个目录中的文件夹,D:\.搜索需要很长时间,如果驱动器有很多子目录,甚至需要5分钟.如何更快地搜索Lib,以便在几秒钟内完成?
我的代码看起来像这样:
find( \&Lib_files, $dir);
sub Lib_files
{
return unless -d;
if ($_=~m/^([L|l]ib(.*))/)
{
print"$_";
}
return;
}
Run Code Online (Sandbox Code Playgroud) 保留带有未定义值的选项(在本例中为'maxdepth')是否可以?
#!/usr/bin/env perl
use warnings;
use 5.012;
use File::Find::Rule::LibMagic qw(find);
use Getopt::Long qw(GetOptions);
my $max_depth;
GetOptions ( 'max-depth=i' => \$max_depth );
my $dir = shift;
my @dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
say for @dbs;
Run Code Online (Sandbox Code Playgroud)
或者我应该这样写:
if ( defined $max_depth ) {
@dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
} else {
@dbs = find( file => magic => 'SQLite*', in => $dir …Run Code Online (Sandbox Code Playgroud) 我发现背后的设计选择File::Find::find有点令人惊讶.我遇到的所有示例都find在void上下文中使用.
该文档还阐明了\&wantedcoderef in find( \&wanted, @dirs )并不是一个过滤器(强调我自己):
该
wanted()函数对每个 文件和目录执行您想要的任何验证.需要注意的是,尽管它的名字,该wanted()函数是一个通用的回调函数,也不能告诉File::Find,如果一个文件被"通缉令"与否.实际上,它的返回值被忽略了.
但是,如果我想以类似的方式将其用作过滤器grep怎么办?我很想知道是否有另一种方法来编写以下内容:
use strict;
use warnings;
use feature 'say';
use File::Find;
my $wanted = qr/^\d{2}_/; # e.g.
my @wanted;
find( sub { -f && /$wanted/ && push @wanted, $_ }, '.' );
# I wish my @wanted = find( ... ); worked
say for @wanted;
Run Code Online (Sandbox Code Playgroud) 我有如下代码.如果我在我的函数中打开文件$File::Find::name(在这种情况下它是./tmp/tmp.h),它会说"无法打开文件./tmp/tmp.h reason =在temp.pl第36行第98行没有这样的文件或目录".searchFile::Find::find
如果我直接在另一个函数中打开文件,我可以打开该文件.有人能告诉我这种行为的原因吗?我在Windows上使用activeperl,版本是5.6.1.
use warnings;
use strict;
use File::Find;
sub search
{
return unless($File::Find::name =~ /\.h\s*$/);
open (FH,"<", "$File::Find::name") or die "cannot open the file $File::Find::name reason = $!";
print "open success $File::Find::name\n";
close FH;
}
sub fun
{
open (FH,"<", "./tmp/tmp.h") or die "cannot open the file ./tmp/tmp.h reason = $!";
print "open success ./tmp/tmp.h\n";
close FH;
}
find(\&search,".") ;
Run Code Online (Sandbox Code Playgroud) 我在文本文件上使用File :: Find和file i/o来解析一系列目录并将内容移动到一个新文件夹中.这是一个简单的脚本(见下文):
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Copy;
my $dir = "/opt/CollectMinderDocuments/coastalalglive"; #base directory for Coastal documents
#read file that contains a list of closed IDs
open(MYDATA, "Closed.txt");
mkdir("Closed");
while(my $line = <MYDATA>) {
chomp $line;
my $str = "$dir" . "/Account$line";
print "$str\n";
find(\&move_documents, $str);
}
sub move_documents {
my $smallStr = substr $File::Find::name, 43;
if(-d) {
#system("mkdir ~/Desktop/Closed/$smallStr");
print "I'm here\n";
system("mkdir /opt/CollectMinderDocuments/coastalalglive/Closed/$smallStr");
#print "Made a directory: /opt/CollectMinderDocuments/coastalalglive/Closed/$smallStr\n";
}
else {
print "Now …Run Code Online (Sandbox Code Playgroud) 有谁知道如何使File:Find可以搜索symlink目录?
我在一个真实的目录
/home/alex/mydir1
Run Code Online (Sandbox Code Playgroud)
和里面的一个符号链接目录
/home/alex/mydir1/test -> ../mydir2
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#!/depot/perl-5.8.3/bin/perl
use strict;
use File::Find qw(find);
my $path = "/home/alex/mydir1";
find(\&Search,follow => 1, $path);
sub Search{
my $path = $File::Find::name;
print $path."\n";
}
Run Code Online (Sandbox Code Playgroud)
结果是:
/home/alex/mydir1
/home/alex/mydir1/test
Run Code Online (Sandbox Code Playgroud)
为什么不通过/ home / alex / mydir2搜索并打印出其中的每个文件?谁能告诉我该怎么做?
感谢你并致以真诚的问候。
亚历克斯
我想列出包含所需目录的目录路径.
例如:
/usr1
|
|
-----------------------------------------
| |
/local1 /local2
| |
dir1 dir1
Run Code Online (Sandbox Code Playgroud)
我想使用通配符找到dir1所在的目录路径*.
从linux命令行我可以这样做以获得结果.
find /usr1/local* -name dir1 -type d
Run Code Online (Sandbox Code Playgroud)
然后就会显示出来
/usr1/local1/dir1
/usr1/local2/dir1
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用File :: Find perl模块.
我不想使用system或`` 完成它.