File::Find

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结尾的文件。
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. find(\&wanted,'/root/learnperl/perltest');
  6. sub wanted {
  7.         print $File::Find::name,"\n" if ($_ =~ /\.sh$/);
  8. }
其中有三个变量:
1.$File::Find::dir  :当前处理的目录名
2.$_                :当前处理的文件名
3.$File::Find::name :目录加文件名
wanted函数是一个回调函数。回调函数是指:一般不自己调用而是由别的程序调用,这里是由find调用。find两个参数第一个是对遍历到文件的行为,一个是待查找的目录。
也可以不用函数名直接通过匿名函数的方式为find添加行为参数。
  1. use File::Find;
  2. my @starting_directories = qw(.);
  3. find(
  4. sub {
  5. print "$File::Find::name found\n";
  6. },
  7. @starting_directories,
  8. );
finddepth 函数与find差不多只有subtle difference。
find是在进行目录测试后在进行目录内容的测试。从目录树根往下。
finddepth 是先内容后目录,从叶子往树根。
可以在代码中加print语句看出。
查找目录:
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. my @find_in_dir = qw{
  6.         /root/learnperl/perltest
  7. };
  8. finddepth(\&wanted,@find_in_dir);
  9. sub wanted {
  10.         print $File::Find::name,"\n" if -d;
  11.         print "just test!\n"; #可以看出finddepth与find的不同
  12. }
处理文件时过滤掉某个目录对该目录及该目录中的都不进行处理。
  1. 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.
    1. 版主的原话:
    2. preprocess => sub { $File::Find::dir eq '/root' ? grep { $_ ne 'test' } @_ : @_ },
    3. 大概意思就是说,preprocess 接受一个文件/目录清单[1](刚刚由 readdir 得到),然后需要返回一个待遍历的文件/目录清单。
    4. 所以在这里可以控制遍历的顺序(通过 sort),或者排除一些目录或者文件。
    5. [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
  1. cat a
  2. #!/usr/bin/perl
  3. use warnings;
  4. use strict;
  5. use File::Find;
  6. my @find_in_dir = qw{
  7.         /root/learnperl/perltest
  8. };
  9. sub wanted{
  10.         print $File::Find::name,"\n";
  11. }
  12. sub filter {
  13.         ($File::Find::dir =~ m{perltest}) ? grep {$_ !~ /mulu1/} @_ :@_;
  14. }
  15. my %option =(
  16.         wanted => \&wanted,
  17.         preprocess => \&filter,
  18. );
  19. find(\%option,@find_in_dir);
查找三天前的文件:
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. my @find_in_dir = qw{
  6.         /root/learnperl/perltest
  7. };
  8. sub wanted{
  9.         print $File::Find::name,"\n" if (-and -M _ > 3);
  10. }
  11. find(\&wanted,@find_in_dir);
File::Find module不仅可以用来查找指定文件也可以用来遍历处理文件。只要灵活运用,会发现它的用处挺多的。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值