# How to resolve a Unix symlink?

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

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:

...

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

Clear@readlink


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"]


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.

• 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, 2013 at 21:15

FileInformation["filename", "AbsoluteFileName"]

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

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