How to get the fully resolved path of a symbolic link in Terminal?
Solution 1:
I think that pwd -P
and readlink
are going to be your friends for this task.
"How can I get the behavior of GNU's readlink -f on a Mac?" is a handy resource.
pwd -P
only works if you're inside the symlink directory:
14:07:13 jason@mac ~ $ cd bin
14:08:08 jason@mac bin $ pwd -P
/Users/jason/Applications
readlink
works by specifying the target (thus it can be used against files):
14:09:03 jason@mac ~ $ readlink bin
Applications
14:09:34 jason@mac ~ $ readlink /var
private/var
The output of readlink
appears to be relative to the parent of the specified target.
Ex: The parent of /var
is /
, so private/var
is correct, relative to /
. Per my above example of bin -> Applications
, both are in my Home Directory, no matter where I run it, the output is the same.
Solution 2:
This resolves recursively and returns an absolute path:
$ python
>>> import os
>>> os.path.realpath("/usr/local/bin/python3")
Or the non-interactive version:
python -c "import os; print(os.path.realpath('/usr/local/bin/python3'))"
Solution 3:
Maybe this discussion on Stack Overflow is of help: How to resolve symbolic links in a shell script In particular, please consider this answer.