Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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.

share|improve this question
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. mydirectory/*
share|improve this answer
I got this error: Type of arg 1 to keys must be hash (not anonymous hash ({})) at /home/alex/bin/ line 3, near "};" – fortran Dec 9 '11 at 9:56
@fortran: Try updated script :| – grawity Dec 9 '11 at 14:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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