- 论坛徽章:
- 3
|
本帖最后由 grshrd49 于 2013-07-19 12:23 编辑
觉得好玩就写个了
运行需要两个参数 第一个是起始目录 第二个是目标文件或目录名- #!/opt/ActivePerl-5.12/bin/perl
- #根据大神的建议,去除了函数中的带名字的函数
- #将特殊变量$a改成e_path,并初始化
- use strict;
- use Cwd;
- my $source_dir = shift @ARGV; #从什么目录开始查找
- my $tag = shift @ARGV; #要查找目录或者文件名
- ©dir($source_dir);
- print "search end!!\n";
- sub copydir
- {
- my $up_dir = shift @_;
- chdir $up_dir;
- my @mydir = <$up_dir\/*>;
- my @myfile = <*>;
- my $fileindex = -1;
- for my $e_path (@mydir){
- $fileindex++;
- #print "$e_path----$myfile[$fileindex]\n";
- next if (-l $e_path);
- if (-d $e_path){
- if ( $myfile[$fileindex] eq $tag ){
- my $path = getcwd;
- print "-- searched dir ->$myfile[$fileindex]<-\n";
- #print "-- path = $path\/$myfile[$fileindex]\n\n"; #问题在这里
- print "-- path = $e_path\n\n";
- }
- ©dir($e_path);
- }else{
- if ( $myfile[$fileindex] eq $tag ){
- my $path = getcwd;
- print "-- searched file ->$myfile[$fileindex]<-\n";
- print "-- path = $e_path\n\n";
- }
- }
- }
- }
- __END__
- #!/opt/ActivePerl-5.12/bin/perl
- use strict;
- use Cwd;
- my $from_dir = shift @ARGV; #从什么目录开始查找
- my $tag = shift @ARGV; #要查找目录或者文件名
- &search($from_dir,$tag);
- sub search
- {
- my $source_dir = shift @_;
- my $tag = shift @_;
- ©dir($source_dir);
- print "search end!!\n";
- sub copydir
- {
- my $up_dir = shift @_;
- chdir $up_dir;
- my @mydir = <$up_dir\/*>;
- my @myfile = <*>;
- my $fileindex = -1;
- for $a (@mydir){
- $fileindex++;
- print "$a----$myfile[$fileindex]\n";
- next if (-l $a);
- if (-d $a){
- if ( $myfile[$fileindex] eq $tag ){
- my $path = getcwd;
- print "-- searched dir ->$myfile[$fileindex]<-\n";
- #print "-- path = $path\/$myfile[$fileindex]\n\n"; #问题在这里
- print "-- path = $a\n\n";
- }
- ©dir($a);
- }else{
- if ( $myfile[$fileindex] eq $tag ){
- my $path = getcwd;
- print "-- searched file ->$myfile[$fileindex]<-\n";
- print "-- path = $a\n\n";
- }
- }
- }
- }
- }
复制代码 |
|