Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Does Mathematica have any function to resolve a Unix symlink (recursively, if necessary) to its real path?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

If Mathematica doesn't have one or if its existence is not immediately evident from the documentation, you can easily whip up an equivalent in under a minute using the operating system's tools. We'll use readlink to resolve the symlink using the -e option:

readlink - print value of a symbolic link or canonical file name
...

-e, --canonicalize-existing
canonicalize by following every symlink in every component of the given name recursively, all components must exist

Clear@readlink
readlink[link_String] := Import["!readlink -e " <> link, "Text"]

Mac users might want to install GNU coreutils and use greadlink instead of the default that ships with Apple (you might also have to replace the executable above with its full path). As an example, on my system:

readlink["~/.vimrc"]
(* "/Users/username/.vim/vimrc" *)

If no output is returned, the input was not a valid symbolic link. You can use this to extend the above definition to include error messages, warnings, etc.

share|improve this answer
    
Thanks. That works well interactively, though it produces a strange error if I run it at startup from my Kernel/init.m. I posted a question about it. –  kjo Jul 24 '13 at 21:15
add comment

FileInformation["filename", "AbsoluteFileName"]

Complete list of file system object properties: FileInformation["filename","Rules"]

share|improve this answer
    
I'm not running a Unix OS but +1 for FileInformation! –  Mr.Wizard Aug 28 '13 at 6:04
    
FileInformation with AbsoluteFileName works on Windows as well, resolving Windows Vista/7 symlinks, NTFS junction points, and SUBST drive links. –  librik Aug 28 '13 at 6:27
    
librik, I did not mean to imply it didn't; I'm very glad you shared this function! Rather, I meant that despite the fact that this question centers on Unix, and therefore I cannot test if it is applicable there, I was grateful enough to vote anyway. –  Mr.Wizard Aug 28 '13 at 6:32
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.