如何使用 File::Find 仅打印具有相对路径的文件?

发布于 2024-08-10 21:23:46 字数 641 浏览 4 评论 0原文

我在下面的代码中使用 File::Find 来查找 /home/user/data 路径。

use File::Find;

my $path = "/home/user/data";
chdir($path);
my @files;

find(\&d, "$path");

foreach my $file (@files) {
print "$file\n";
}

sub d {
-f and -r and push  @files, $File::Find::name;
}

当我将目录路径更改为我需要搜索文件的路径时,它仍然为我提供了带有完整路径的文件。即,

/home/user/data/dir1/file1
/home/user/data/dir2/file2
and so on...

但我想要类似的输出,

dir1/file1
dir2/file2
and so on...

有人可以建议我查找文件并仅从当前工作目录显示的代码吗?

I am using File::Find in the code below to find the files from /home/user/data path.

use File::Find;

my $path = "/home/user/data";
chdir($path);
my @files;

find(\&d, "$path");

foreach my $file (@files) {
print "$file\n";
}

sub d {
-f and -r and push  @files, $File::Find::name;
}

As I am changing the dir path to the path from where i need to search the files but still it gives me the files with full path. i.e.

/home/user/data/dir1/file1
/home/user/data/dir2/file2
and so on...

but I want the output like

dir1/file1
dir2/file2
and so on...

Can anyone please suggest me the code to find the files and display from current working directory only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找个人就嫁了吧 2024-08-17 21:23:46

下面将打印 $base 下所有文件相对于 $base 的路径(不是当前目录):

#!/usr/bin/perl
use warnings;
use strict;

use File::Spec;
use File::Find;

# can be absolute or relative (to the current directory)
my $base = '/base/directory';
my @absolute;

find({
    wanted   => sub { push @absolute, $_ if -f and -r },
    no_chdir => 1,
}, $base);

my @relative = map { File::Spec->abs2rel($_, $base) } @absolute;
print $_, "\n" for @relative;

The following will print the path for all files under $base, relative to $base (not the current directory):

#!/usr/bin/perl
use warnings;
use strict;

use File::Spec;
use File::Find;

# can be absolute or relative (to the current directory)
my $base = '/base/directory';
my @absolute;

find({
    wanted   => sub { push @absolute, $_ if -f and -r },
    no_chdir => 1,
}, $base);

my @relative = map { File::Spec->abs2rel($_, $base) } @absolute;
print $_, "\n" for @relative;
半透明的墙 2024-08-17 21:23:46

直接删除它怎么样:

foreach my $file (@files) {
$file =~ s:^\Q$path/::;
print "$file\n";
}

注意:这实际上会更改@files的内容。

根据评论,这不起作用,所以让我们测试一个完整的程序:

#!/usr/local/bin/perl
use warnings;
use strict;
use File::Find;

my $path = "/usr/share/skel";
chdir($path);
my @files;

find(\&d, "$path");

foreach my $file (@files) {
$file =~ s:^\Q$path/::;
print "$file\n";
}

sub d {
-f and -r and push  @files, $File::Find::name;
}

我得到的输出是

$ ./find.pl
dot.cshrc
dot.login
dot.login_conf
dot.mailrc
dot.profile
dot.shrc

这对我来说似乎工作正常。我也用带子目录的目录测试过,没有问题。

How about just removing it:

foreach my $file (@files) {
$file =~ s:^\Q$path/::;
print "$file\n";
}

Note: this will actually change the contents of @files.

According to comments this doesn't work, so let's test a full program:

#!/usr/local/bin/perl
use warnings;
use strict;
use File::Find;

my $path = "/usr/share/skel";
chdir($path);
my @files;

find(\&d, "$path");

foreach my $file (@files) {
$file =~ s:^\Q$path/::;
print "$file\n";
}

sub d {
-f and -r and push  @files, $File::Find::name;
}

The output I get is

$ ./find.pl
dot.cshrc
dot.login
dot.login_conf
dot.mailrc
dot.profile
dot.shrc

This seems to be working OK to me. I've also tested it with directories with subdirectories, and there is no problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文