I have different symbolic links aliases for a set of files (beware, some symbolic links might point to other symbolic links in the same directory) and I would like to get a list of unique files they are pointing to.

Example:

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

In the end I would like to have /somedir/a /somedir/b /somedir/c to be passed as arguments to another command.

link|improve this question
feedback

1 Answer

up vote 1 down vote accepted

The libc realpath() function can do most of the work by resolving all symlinks; write a script similar to this to filter unique results:

#!/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";

File list is taken from command-line arguments.

uniquefiles.pl mydirectory/*
link|improve this answer
I got this error: Type of arg 1 to keys must be hash (not anonymous hash ({})) at /home/alex/bin/uniquefiles.pl line 3, near "};" – fortran Dec 9 '11 at 9:56
@fortran: Try updated script :| – grawity Dec 9 '11 at 14:52
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.