1.$File::Find::dir :当前处理的目录名
2.$_ :当前处理的文件名
3.$File::Find::name :目录加文件名
#!/usr/bin/perl
use File::Find;
$t_path = '/home/zl/temp';
find(\&wanted, $t_path);
sub wanted(){
if( -f $File::Find::name){
print $File::Find::name."\n";
print $File::Find::dir."\n";
print $_."\n";
}
}
注: if ( -f $File::Find::name ) -f判断是file
结果:
zl@zl-vm:~/study/perl/find$ perl find.pl
/home/zl/temp/ifconfig.txt
/home/zl/temp
ifconfig.txt
/home/zl/temp/date.txt
/home/zl/temp
date.txt
参考 : http://blog.chinaunix.net/uid-24504673-id-112319.html
简单记录下File::Find:
在目录/root/learnperl/perltest 下寻找以.sh结尾的文件。
- #!/usr/bin/perl
- use warnings;
- use strict;
- use File::Find;
- find(\&wanted,'/root/learnperl/perltest');
- sub wanted {
- print $File::Find::name,"\n" if ($_ =~ /\.sh$/);
- }
其中有三个变量:
1.$File::Find::dir :当前处理的目录名
2.$_ :当前处理的文件名
3.$File::Find::name :目录加文件名
wanted函数是一个回调函数。回调函数是指:一般不自己调用而是由别的程序调用,这里是由find调用。find两个参数第一个是对遍历到文件的行为,一个是待查找的目录。
也可以不用函数名直接通过匿名函数的方式为find添加行为参数。
- use File::Find;
- my @starting_directories = qw(.);
- find(
- sub {
- print "$File::Find::name found\n";
- },
- @starting_directories,
- );
finddepth 函数与find差不多只有subtle difference。
find是在进行目录测试后在进行目录内容的测试。从目录树根往下。
finddepth 是先内容后目录,从叶子往树根。
可以在代码中加print语句看出。
查找目录:
- #!/usr/bin/perl
- use warnings;
- use strict;
- use File::Find;
- my @find_in_dir = qw{
- /root/learnperl/perltest
- };
- finddepth(\&wanted,@find_in_dir);
- sub wanted {
- print $File::Find::name,"\n" if -d;
- print "just test!\n"; #可以看出finddepth与find的不同
- }
处理文件时过滤掉某个目录对该目录及该目录中的都不进行处理。
- The value should be a code reference. This code reference is used to preprocess the current directory. The name of the currently processed directory is in $File::Find::dir. Your preprocessing function is called after readdir(), but before the loop that calls the wanted() function. It is called with a list of strings (actually file/directory names) and is expected to return a list of strings. The code can be used to sort the file/directory names alphabetically, numerically, or to filter out directory entries based on their name alone. When follow or follow_fast are in effect, preprocess is a no-op.
-
- 版主的原话:
- preprocess => sub { $File::Find::dir eq '/root' ? grep { $_ ne 'test' } @_ : @_ },
- 大概意思就是说,preprocess 接受一个文件/目录清单[1](刚刚由 readdir 得到),然后需要返回一个待遍历的文件/目录清单。
- 所以在这里可以控制遍历的顺序(通过 sort),或者排除一些目录或者文件。
- [1]:注意这个清单不包括路径,当前路径在 $File::Find::dir 变量中。
[root@PC_IN_LAN perltest]#pwd
/root/learnperl/perltest
[root@PC_IN_LAN perltest]#find . -maxdepth 1 -type f | xargs -i rm -f {};
[root@PC_IN_LAN perltest]#ls -R
.:
mulu1 mulu2 mulu3 mulu4
./mulu1:
file1.txt
./mulu2:
file2.txt
./mulu3:
file3.txt
./mulu4:
file4.txt
[root@PC_IN_LAN perltest]#../a
/root/learnperl/perltest
/root/learnperl/perltest/mulu2
/root/learnperl/perltest/mulu2/file2.txt
/root/learnperl/perltest/mulu4
/root/learnperl/perltest/mulu4/file4.txt
/root/learnperl/perltest/mulu3
/root/learnperl/perltest/mulu3/file3.txt
- cat a
- #!/usr/bin/perl
- use warnings;
- use strict;
- use File::Find;
- my @find_in_dir = qw{
- /root/learnperl/perltest
- };
- sub wanted{
- print $File::Find::name,"\n";
- }
- sub filter {
- ($File::Find::dir =~ m{perltest}) ? grep {$_ !~ /mulu1/} @_ :@_;
- }
- my %option =(
- wanted => \&wanted,
- preprocess => \&filter,
- );
- find(\%option,@find_in_dir);
查找三天前的文件:
- #!/usr/bin/perl
- use warnings;
- use strict;
- use File::Find;
- my @find_in_dir = qw{
- /root/learnperl/perltest
- };
- sub wanted{
- print $File::Find::name,"\n" if (-f and -M _ > 3);
- }
- find(\&wanted,@find_in_dir);
File::Find module不仅可以用来查找指定文件也可以用来遍历处理文件。只要灵活运用,会发现它的用处挺多的。