0

Is there possibility of retrieving absolute path of symbolic file in bash? I cannot use realpath() and readlink() gives path of target. But i need absolute path of that symlink.

1 Answers1

0

Is this an assignment? I can't imagine a real situation where you would not be allowed to use readlink but anyway, just for fun, here is a hack script to get the full path of the symlink provided as argument:

For the example I will use the symlink pidof which points to /bin/ usually, but is a symlink to /sbin/killall5 (Ubuntu)

#!/bin/bash

linkpath="$(which $1)"

line="$(ls -la $linkpath)"

realpath="$(echo $line | awk '{print $NF}')"

echo "Full path is: $realpath"

exit

Output:

user@system:~/Desktop$ ./test.sh pidof
Full path is: /sbin/killall5
user@system:~/Desktop$ 

The reason this works is because the output of ls shows the full path of symlinks. (At least in Debian distros)

asimovwasright
  • 838
  • 1
  • 11
  • 28