Size: 729
Comment:
|
← Revision 7 as of 2013-07-26 22:44:57 ⇥
Size: 980
Comment: simplify `perl`, add missing `--`, `-d` won't harm here either.
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
[[Anchor(faq29)]] == 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. |
<<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: |
Line 10: | Line 10: |
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 -le 'print readlink "/bin/sh"' }}} |
Line 12: | 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: | |
Line 19: | Line 23: |
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 25: |
# Bash | |
Line 25: | Line 30: |
ll="$(LC_ALL=C ls -l "$path" 2> /dev/null)" && echo "${ll/* -> }" |
ll=$(LC_ALL=C ls -ld -- "$path" 2>/dev/null) && printf '%s\n' "${ll#* -> }" |
Line 32: | Line 37: |
However, this can fail if a symbolic link contains " -> " in its name. ---- 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 -le 'print readlink "/bin/sh"'
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 -ld -- "$path" 2>/dev/null) && printf '%s\n' "${ll#* -> }" else return 1 fi }
However, this can fail if a symbolic link contains " -> " in its name.