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?
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.
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.
file -b will give you the type of file.
file -b a_file_thats_actually_a_symlink
will output
symbolic link to `<actual file>'
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
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.
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"; }
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";
}