I can show the target file that a link points to using ls -l
:
snowch$ ls -l /usr/local/bin/mvn
lrwxr-xr-x 1 snowch admin 29 12 Dec 08:58 /usr/local/bin/mvn -> ../Cellar/maven/3.2.3/bin/mvn
Is there a way to show less output without having to pipe through another command such as awk? E.g:
snowch$ ls ?? /usr/local/bin/mvn
/usr/local/bin/mvn -> ../Cellar/maven/3.2.3/bin/mvn
I'm running 3.2.53 on OS X 10.9.5. The output from several commands is shown below:
snowch$ ls -H /usr/local/bin/mvn
/usr/local/bin/mvn
snowch$ ls -L /usr/local/bin/mvn
/usr/local/bin/mvn
snowch$ file /usr/local/bin/mvn
/usr/local/bin/mvn: POSIX shell script text executable
snowch$ file -b /usr/local/bin/mvn
POSIX shell script text executable
Best Answer
ls
unfortunately doesn't have an option to retrieve file attributes and display them in an arbitrary way. Some systems have separate commands for that (for instance GNU has astat
command or the functionality in GNUfind
).On most modern systems, with most files, this should work though:
That works by removing the first 8 blank delimited fields of the first line of the output of
ls -l
. That should work except on systems where the gid is not displayed there or the first 2 fields are joined together when there's a large number of links.With GNU
stat
:With GNU
find
:With FreeBSD/OS/X stat:
With
zsh
stat:Many systems also have a
readlink
command to specifically get the target of a link: