简体   繁体   English

Perl:如何使用 File::Find::Rule 列出给定路径中的所有符号链接

[英]Perl: How to use File::Find::Rule to list all symbolic links from a given path

ls /foo/bar/ lrwxr-xr-x a1 -> ../../../a1 lrwxr-xr-x a2 -> ../../../a2 lrwxr-xr-x a3 -> ../../../a3 ls /foo/bar/ lrwxr-xr-x a1 -> ../../../a1 lrwxr-xr-x a2 -> ../../../a2 lrwxr-xr-x a3 -> ../../../a3

This is a curtailed output of ls.这是 ls 的缩减输出。

My goal: 1. Go to /foo/bar/ and find the latest version of a (which is a symbolic link).我的目标: 1. 转到 /foo/bar/ 并找到 a(这是一个符号链接)的最新版本。 So in this case, a3.所以在这种情况下,a3。 Copy the contents of a3 to a temp location将 a3 的内容复制到临时位置

I am trying to use File::Find::Rule but I am unable to figure out how to use it to list all the symbolic links.我正在尝试使用File::Find::Rule但我无法弄清楚如何使用它来列出所有符号链接。 Reading through various Google sites, I see people explaining how to follow the symbolic links but not to list them.浏览各种谷歌网站,我看到有人在解释如何跟踪符号链接但不列出它们。

What I have figured out so far:到目前为止我想出了什么:

my $filePath = "/foo/bar"; my @files = File::Find::Rule->file->in(filePath);

This returns an empty array because there there are no files only symbolic links in /foo/bar.这将返回一个空数组,因为 /foo/bar 中没有文件,只有符号链接。 I also tried my @files = File::Find::Rule->in($makeFilePath)->extras({follow =>1});我也试过my @files = File::Find::Rule->in($makeFilePath)->extras({follow =>1}); but I feel that is asks to follow the symbolic link rather than list them.但我觉得这是要求遵循符号链接而不是列出它们。

Use the symlink method from -X test synonyms provided in File::Find::Rule使用File::Find::Rule 中提供的-X 测试同义词中symlink方法

use warnings 'all';
use strict;

use File::Find::Rule;

my $rule = File::Find::Rule->new;

my @links = $rule->symlink->in('.');

print "@links\n";

This finds all files which satisfy -l file test in the current directory.这将查找当前目录中满足-l file test 的所有文件。 Also see -X .另见-X

With the list of links on hand, you can use the -M file test or stat (or its File::stat by-name interface), to sort it out by timestamps of the target files .有了手头的链接列表,您可以使用-M文件 test 或stat (或其File::stat by-name 接口),按目标文件的时间戳对其进行排序。 For example例如

use List::Util 'max';
my %ts_name = map { (stat)[9] => $_ } @links;
my $latest = $ts_name{ max (keys %ts_name) };

There are other ways to sort/filter/etc a list.还有其他方法可以对列表进行排序/过滤/等。 If you use -M then you need min .如果你使用-M那么你需要min If you wanted the timestamps for the link itself for some reason, use the lstat instead.如果出于某种原因需要链接本身的时间戳,请改用lstat The module also provides an mtime method for work with timestamps, but it is meant for search and not suitable for sorting.该模块还提供了一个用于处理时间戳的mtime方法,但它用于搜索而不适合排序。

Note that you don't have to actually create an object first, but can directly do请注意,您不必先实际创建对象,而是可以直接执行

use File::Find::Rule;
my @links = File::Find::Rule->symlink->in('.');

To copy/move things use core File::Copy , while for temporary files core File::Temp is useful.要复制/移动内容,请使用核心File::Copy ,而对于临时文件,核心File::Temp很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM