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


2 Answers 2


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

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:

(* "/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.

  • $\begingroup$ 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. $\endgroup$
    – kjo
    Jul 24, 2013 at 21:15

FileInformation["filename", "AbsoluteFileName"]

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

  • $\begingroup$ I'm not running a Unix OS but +1 for FileInformation! $\endgroup$
    – Mr.Wizard
    Aug 28, 2013 at 6:04
  • $\begingroup$ FileInformation with AbsoluteFileName works on Windows as well, resolving Windows Vista/7 symlinks, NTFS junction points, and SUBST drive links. $\endgroup$
    – librik
    Aug 28, 2013 at 6:27
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Aug 28, 2013 at 6:32
  • $\begingroup$ I guess the FileInformation function has gone away sicne this answer was posted? $\endgroup$
    – Leon Avery
    Jun 10, 2021 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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