Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have a tangled mess of python installations on my laptop. I was looking at the executables in /usr/local/bin and they are all symbolic links to ../../../Library......

There's some weird behavior surrounding this. If I do ls -lhaG I see ---> and the relative path to the right of the symlinks:

lrwxr-xr-x  1 root  wheel    69B Dec  7 22:29 python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.1/bin/python3

However, I can't seem to get any of the command line tools to resolve that path and display the actual path to python3. I've found some tidbits here and there regarding use of pwd -P and the find utility to do this. The problem is that these don't seem to work on OS X the way that people describe them working on linux (i.e. outputting the full path to the symbolic link). They just print the symbolic links path for me:

FantasticMrFox:bin robert$ pwd -P python3
/usr/local/bin
FantasticMrFox:bin robert$ find `pwd -P` -name python3
/usr/local/bin/python3

Any ideas on what's going on here?

share|improve this question
1  
[offtopic] Congrats on breaking 2K, calavera. –  Jason Salaz Dec 10 '10 at 18:17
1  
I've been working at this for the last half an hour. ls with options, find with options, stat and readlink, have all failed me. :(. Consider this question subscribed. –  Jason Salaz Dec 10 '10 at 18:54
    
@Jason: thanks! –  Robert S Ciaccio Dec 10 '10 at 22:47
1  
Wow. Seeing this question again gave me a serious kick of Déjà Vu... Indeed it is the same one though. –  Jason Salaz Apr 28 '11 at 19:57
add comment

4 Answers

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.

share|improve this answer
    
readlink does not recurse through the case where the symlink points to other symlinks. I would not call that "fully resolved" –  James Roth Dec 11 '13 at 17:12
add comment

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')"
share|improve this answer
    
When builtins fail you, resort to scripting... Nice. Definitely works: >$ python -c "import os; print os.path.realpath('bin')" >/Users/jason/Applications. I symlink ~/bin to ~/Applications in order to have a unified .bash_profile/$PATH across Mac and *nix systems. –  Jason Salaz Dec 10 '10 at 21:34
    
this is great and it works well! I would mark it as the answer except I also want to know if there's a way to do this in the shell like you can on other *nix systems. –  Robert S Ciaccio Dec 24 '10 at 21:19
    
How do you do it on other *nix systems? Also, Python is installed nearly universally on Linux systems nowadays. –  Jason Salaz Dec 26 '10 at 20:16
    
@vxjasonxv the stuff I found at the time I was messing with this suggested that the pwd and find commands I talked about in the question resolve the full path on systems other than OS X. I haven't tested them though. –  Robert S Ciaccio Jan 8 '11 at 22:52
add comment

Maybe this discussion on Stack Overflow is of help: How to resolve symbolic links in a shell script In particular, please consider this answer.

share|improve this answer
add comment

You may use "readlink -f". It recurses through all the links. Suppose the link is to a path that is further a link. You would like to have the final absolute path.

share|improve this answer
    
Huh? What are you exactly saying? –  Rob Jan 27 at 8:01
    
The correct syntax for what @DINESH SEHRA is saying is to use read link -f python3 for example, and it will buffer the full symbolic link for you. –  Danijel J Jan 27 at 8:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.