如何使用 File::Find 仅打印具有相对路径的文件?
我在下面的代码中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面将打印
$base
下所有文件相对于$base
的路径(不是当前目录):The following will print the path for all files under
$base
, relative to$base
(not the current directory):直接删除它怎么样:
注意:这实际上会更改
@files
的内容。根据评论,这不起作用,所以让我们测试一个完整的程序:
我得到的输出是
这对我来说似乎工作正常。我也用带子目录的目录测试过,没有问题。
How about just removing it:
Note: this will actually change the contents of
@files
.According to comments this doesn't work, so let's test a full program:
The output I get is
This seems to be working OK to me. I've also tested it with directories with subdirectories, and there is no problem.