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.
Asked
Active
Viewed 1,925 times
1 Answers
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
-
And i am allowed to use readlink, just not realpath. And yes, its assignment :/ – Tomáš Neumaier Apr 06 '15 at 13:54
-
I know... It was just a hack script for fun. I misread the op, thought he wasn't allowed readlink. – asimovwasright Apr 07 '15 at 07:33