如何过滤重复的符号链接


0

我有一组文件的不同符号链接别名(注意,一些符号链接可能指向同一目录中的其他符号链接),我想得到一个他们指向的唯一文件列表。

例:

alias_a1 -> alias_a2
alias_a2 -> /somedir/a
alias_b1 -> /somedir/b
alias_b2 -> /somedir/b
alias_c1 -> /somedir/c

最后,我希望/somedir/a /somedir/b /somedir/c作为参数传递给另一个命令。

Answers:


1

libc realpath()函数可以通过解析所有符号链接完成大部分工作; 编写与此类似的脚本来过滤唯一结果:

#!/usr/bin/env perl
use Cwd;
my %files = map {Cwd::realpath($_) => 1} @ARGV;
my @files = sort keys %files;

# This example will run: mycommand arg1 arg2 file1 file2 file3 arg3
system {"mycommand"} "mycommand", "arg1", "arg2", @files, "arg3";

文件列表取自命令行参数。

uniquefiles.pl mydirectory/*

我得到了这个错误:Type of arg 1 to keys must be hash (not anonymous hash ({})) at /home/alex/bin/uniquefiles.pl line 3, near "};"
英格兰

@fortran:尝试更新脚本:|
grawity 2011年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.