如何更改脚本以在子文件夹上列出文件?

时间:2019-05-21 15:57:13

标签: list file perl

我想更改脚本以列出子文件夹中的文件。可以说我选择了/ lib64文件夹,y也想在子文件夹中进行搜索 感谢您的帮助。

我需要帮助不仅列出列出的文件夹,而且列出子文件夹

在我的脚本下面,没有任何问题。...

#!/usr/bin/perl -w

system("clear");
my $dev;
my $ino;
my $mode;
my $nlink;
my $uid;
my $gid;
my $rdev;
my $size;
my $atime;
my $mtime;
my $ctime;
my $blksize;
my $blocks;
my $perm;
my $tmp1;
my $tmp2;
clear;

print " \n";
print "Please enter the Directory you want to list : ";
my $dir = <STDIN>;
chomp $dir;
print "Directory selected is ------------> $dir | \n \n";

print "Please enter the minimum size (in bytes) of files you want to list 
: ";
my $sz = <STDIN>;
chomp $sz;
print "Minimal size of files (in bytes)-----------------> $sz | \n \n";
print "...........Please wait, preparing the file listing....... \n \n";

sleep 6;

opendir (DIR, $dir ) or die "Cannot open $dir";

while (my $file = readdir(DIR)) {

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
     $atime,$mtime,$ctime,$blksize,$blocks) = stat("$dir/$file");


$perm = sprintf("%04o", $mode & 07777);
$tmp1 = int(($size/$sz));
$tmp2 = length($file);


if (($tmp1 > $tmp2) && ($perm =~ /.[^5410]../)) {

    print "List of files in --> $dir | ";
    print("File Name: -$file- | Size in bytes -$size- \n");
}
}

closedir(DIR);

1 个答案:

答案 0 :(得分:2)

以下是使用Path::Iterator::Rule进行递归迭代的示例。另外,File::stat是一个更简单的统计字段界面。

use strict;
use warnings;
use Path::Iterator::Rule;
use File::Basename;
use File::stat;

print "Please enter the Directory you want to list : ";
chomp(my $dir = readline *STDIN);
print "Please enter the minimum size (in bytes) of files you want to list : ";
chomp(my $sz = readline *STDIN);

my $rule = Path::Iterator::Rule->new->not_dir->size(">=$sz");
my $next = $rule->iter($dir);
while (defined(my $file = $next->())) {
  my $stat = stat $file or die "Failed to stat $file: $!";
  my $mode = $stat->mode;
  my $size = $stat->size;
  my $basename = basename $file;
  ...
}