views:

5584

answers:

8

Say you have a symbolic link, i.e., a -> b. In *nix, is there a command that will simply output what 'a' points to (i.e., 'b') but with nothing else? Typically we do a ls -l and pipe it to grep or something, but say I don't want to do any parsing. Is there a way to do this?

A: 

'ls -l foo' will show it.

SCdF
A: 

ls -l filename

That's about all you've got. You don't need to use grep then, but if you want just the symlink info you will need to pipe the result to sed, awk or similar. If you really just hate typing sed patterns all day long, you can turn this into a mini script or even an alias (in certain shells), but at some level, text filtering will be required.

Daniel Spiewak
+21  A: 

readlink works on Mac OS X, and (per comments) linux.

It is probably good on most *nix systems.

From the man page (for readlink and stat on my machine):

When invoked as readlink, only the target of the symbolic link is printed.
If the given argument is not a symbolic link, readlink will print nothing and exit with an error.

dmckee
This is the right answer. `readlink` is indeed available on linux.
Daniel Spiewak
Definitely works on Linux, I just tried it on my Debian box.
Adam Pierce
FWIW, /usr/bin/readlink on OSX 10.5 and RHEL 5, /bin/readlink on ubuntu 8.10. I don't have any other systems handy to check.
jtimberman
Does it work in solaris?
PJ
+2  A: 

file -b will give you the type of file.

file -b a_file_thats_actually_a_symlink

will output

symbolic link to `<actual file>'
David Nehme
A: 

I know you said you didn't want to use grep, but I can't think of an easier way.

You can use this command if you want to view only symbolic links in a directory.

ls -l | grep "\->"

Or you can use this command to view all files and symbolic links w/ their destinations.

ls -l
AdamB
+1  A: 

I don't think this will be exactly what you're looking for, but the following will show just the targets for all symlinks in the current directory:

find . -maxdepth 1 -type l -printf '%l\n'

It doesn't show any other files though. You could include subdirectories and files in the listing by using this monstrosity:

find . -maxdepth 1 -type l -printf '%l\n' -o -type f -print -o -type d -print

But it'll slap ./ in front of everything, so it's not pretty.

yukondude
+1  A: 

You can always write your own tools. Here's something to get you started. It doesn't have to be Perl, but you'd do the same thing in whatever favorite language you like. Customize it how you like to get exactly what you want.

#!/usr/bin/perl

opendir DIR, $ARGV[0] or die "Could not open directory: $!";

while( my $file = readdir DIR )
    {
    next unless -l $file;

    my $target = readlink( $file );

    print "$file -> $target\n";
    }
brian d foy
+1  A: 

readlink - display target of symbolic link on standard output

Used in a loop to find the destination.

#!/usr/bin/perl

use strict;

while(chomp(my $file = <>))
{
        while(-l $file)
        {
                $file   = readlink $file;
        }
        print "$file\n";
}
Martin York
Heh, that's pretty cool. I hadn't considered multiple levels of links and had to think about that inner while() for a moment. :)
brian d foy