ITPub博客

首页 > Linux操作系统 > Linux操作系统 > recursively list files for a given directory

recursively list files for a given directory

原创 Linux操作系统 作者:cqubityj 时间:2009-09-30 15:14:15 0 删除 编辑
#!/usr/bin/perl -w

use strict;

sub recurse($) {
  my($path) = @_;

  ## append a trailing / if it's not there
  $path .= '/' if($path !~ /\/$/);

  ## print the directory being searched
  print $path,"\n";

  ## loop through the files contained in the directory
  for my $eachFile (glob($path.'*')) {

    ## if the file is a directory
    if( -d $eachFile) {
      ## pass the directory to the routine ( recursion )
      recurse($eachFile);
    } else {

      ## print the file ... tabbed for readability
      print "\t",$eachFile,"\n";
    }
  }
}

## initial call ... $ARGV[0] is the first command line argument
recurse($ARGV[0]);


使用方法:script.pl  /xxx/yyy/zzz

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/228190/viewspace-615911/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-19

  • 博文量
    133
  • 访问量
    454950