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

this curtailed output of ls.

my goal: 1. go /foo/bar/ , find latest version of (which symbolic link). in case, a3. copy contents of a3 temp location

i trying use file::find::rule unable figure out how use list symbolic links. reading through various google sites, see people explaining how follow symbolic links not list them.

what have figured out far:

my $filepath = "/foo/bar"; @files = file::find::rule->file->in(filepath);

this returns empty array because there there no files symbolic links in /foo/bar. tried my @files = file::find::rule->in($makefilepath)->extras({follow =>1}); feel asks follow symbolic link rather list them.

use symlink method -x test synonyms provided in file::find::rule

use warnings 'all'; use strict;  use file::find::rule;  $rule = file::find::rule->new;  @links = $rule->symlink->in('.');  print "@links\n"; 

this finds files satisfy -l file test in current directory. see -x.

with list of links on hand, can use -m file test or stat (or file::stat by-name interface), sort out timestamps of the target files. example

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

there many other ways sort/filter/etc list. if use -m need min. if wanted timestamps for link itself reason, use lstat instead. module provides mtime method work timestamps, meant search , not suitable sorting.

note don't have create object first, can directly do

use file::find::rule; @links = file::find::rule->symlink->in('.'); 

to copy/move things use core file::copy, while temporary files core file::temp useful.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -