Differences between revisions 1 and 2
Revision 1 as of 2007-05-02 23:17:12
Size: 729
Editor: redondos
Comment:
Revision 2 as of 2008-05-16 17:40:19
Size: 951
Editor: GreyCat
Comment: clean up, add perl alternative
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== How can I display value of a symbolic link on standard output? ==
The
external command {{{readlink}}} can be used to display the value of a symbolic link.
== How can I display the target of a symbolic link? ==
The n
onstandard external command {{{readlink(1)}}} can be used to display the target of a symbolic link:
Line 10: Line 9:
you can also use GNU find's %l directive, which is especially useful if you need to resolve links in batches: If you don't have `readlink`, you can use Perl:
{{{
perl -e 'print readlink "/bin/sh", "\n"'
}}}
Line 12: Line 14:
You can also use GNU [:UsingFind:find]'s `-printf %l` directive, which is especially useful if you need to resolve links in batches:
Line 19: Line 22:
If your system lacks {{{readlink}}}, you can use a function like this one: If your system lacks both {{{readlink}}} and Perl, you can use a function like this one:
Line 21: Line 24:
# Bash
Line 32: Line 36:

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

Anchor(faq29)

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
bash

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

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

You can also use GNU [:UsingFind: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/* -> }"
    else
        return 1
    fi
}

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

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