Differences between revisions 2 and 5 (spanning 3 versions)
Revision 2 as of 2008-05-16 17:40:19
Size: 951
Editor: GreyCat
Comment: clean up, add perl alternative
Revision 5 as of 2009-12-30 18:29:00
Size: 977
Editor: MatthiasPopp
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq29)]] <<Anchor(faq29)>>
Line 4: Line 4:
Line 14: Line 15:
You can also use GNU [:UsingFind:find]'s `-printf %l` directive, which is especially useful if you need to resolve links in batches: You can also use GNU [[UsingFind|find]]'s `-printf %l` directive, which is especially useful if you need to resolve links in batches:
Line 38: Line 39:

----
CategoryShell

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 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.


CategoryShell

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