Find only destination of symlink

Another option would be to use the specifically designed command readlink if available.

E.g.

$ readlink -f `command -v php`
/usr/bin/php7.1

On Mac OS X and FreeBSD/NetBSD/etc. it's:

stat -f %Y <filename>

More generically I guess the solution is (stat --printf=%N uses weird quotes):

ls -l b | sed -e 's/.* -> //'

Example:

# ln -s a b
# stat -f %Y b
a

Another method is:

# find b -maxdepth 0 -printf %l
a#

The last line is mangled because it has no newline, but that is fine if you need the result in a variable, like so

# f=$(find b -maxdepth 0 -printf %l)
# echo $f
a

The -maxdepth is needed to prevent find from descending into directories if b happens to be a directory.


This can be done using GNU find: find src -prune -printf "%l\n".