【问题标题】:How can I print a list of all files in a directory with their full path?如何使用完整路径打印目录中所有文件的列表?
【发布时间】:2013-07-29 20:12:54
【问题描述】:

我有什么: 我有一个文件夹,其中包含许多其他文件夹和文件。我需要获取名为 l1 的文件夹中所有文件的路径列表。在我的主目录中有许多不同的名为 l1 的文件夹,因此我必须搜索每个 l1 文件夹并返回其中每个文件的路径。我已经能够打印所有 l1 文件夹位置的列表,但我不知道如何列出这些位置中的文件。我用于查找所有 l1 文件夹位置的代码如下。

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;
my @folder;
sub wanted {
    if ( -d && $_ eq 'l1' ) {
        push @folder, $File::Find::name;
    }
}
find \&wanted, '/mnt/vbox_share/';
open fp, ">process.txt";
print fp "@folder";

我需要修改或添加什么才能列出我搜索的文件夹中的所有文件路径?

【问题讨论】:

    标签: perl file directory


    【解决方案1】:

    这就是我们所需要的。

    • wanted 忽略除文件之外的所有内容,丢弃目录和链接

    • 执行wanted时文件的完整路径在$File::Find::name

    • / 上拆分它并取最后一个元素$path[-2] 找到父目录的名称

    • print 如果该目录等于l1,则为完整文件路径

    .

    use strict;
    use warnings;
    
    use File::Find;
    
    find( \&wanted, '/path/to/root/dir');
    
    sub wanted {
      return unless -f;
      my @path = split /\//, $File::Find::name;
      print $File::Find::name, "\n" if @path > 1 and $path[-2] eq 'l1';
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 2021-06-11
      • 2023-01-17
      • 1970-01-01
      • 2011-11-23
      • 2017-04-03
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多