Differences between revisions 3 and 4
Revision 3 as of 2008-11-22 14:09:26
Size: 952
Editor: localhost
Comment: converted to 1.6 markup
Revision 4 as of 2008-11-22 21:35:56
Size: 954
Editor: GreyCat
Comment: first-line
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:

How can I display the target of a symbolic link?

The nonstandard external command readlink(1) can be used to display the target of a symbolic link:

$ readlink /bin/sh

If you don't have readlink, you can use Perl:

perl -e 'print readlink "/bin/sh", "\n"'

You can also use GNU find's -printf %l directive, which is especially useful if you need to resolve links in batches:

$ find /bin/ -type l -printf '%p points to %l\n'
/bin/sh points to bash
/bin/bunzip2 points to bzip2

If your system lacks both readlink and Perl, you can use a function like this one:

# Bash
readlink() {
    local path=$1 ll

    if [ -L "$path" ]; then
        ll="$(LC_ALL=C ls -l "$path" 2> /dev/null)" &&
        echo "${ll/* -> }"
        return 1

However, this can fail if a symbolic link contains " -> " in its target.

BashFAQ/029 (last edited 2013-07-26 22:44:57 by StephaneChazelas)