FILE_READLINK

The FILE_READLINK function returns the path pointed to by UNIX symbolic links.

Examples

Under macOS, the /etc directory is actually a symbolic link. The following statement reads it and returns the location to which the link points:

path = FILE_READLINK('/etc')

It is possible to have chains of symbolic links, each pointing to another. The following function uses FILE_READLINK to iteratively translate such links until it finds the actual file:

FUNCTION RESOLVE_SYMLINK, path

  savepath = path      ; Remember last successful translation
  WHILE (path NE '') DO BEGIN
    path = FILE_READLINK(path, /ALLOW_NONEXISTENT, $       /ALLOW_NONSYMLINK)
    IF (path NE '') THEN BEGIN
      ; If returned path is not absolute, use it to replace the
      ; last path segment of the previous path.
      IF (STRMID(path, 0, 1) NE '/') THEN BEGIN
        last = STRPOS(savepath, '/', /REVERSE_SEARCH)
        IF (last NE -1) THEN path = STRMID(savepath, 0, last) $
          + '/' + path
      ENDIF
      savepath = path
    ENDIF
  ENDWHILE

  ; FILE_EXPAND_PATH removes redundant things like /./ from
  ; the result.
  RETURN, FILE_EXPAND_PATH(savepath)

END

Syntax

Result = FILE_READLINK(Path [, /ALLOW_NONEXISTENT] [, /ALLOW_NONSYMLINK] [, /NOEXPAND_PATH] )

Return Value

Returns the path associated with a symbolic link.

Arguments

Path

A scalar string or string array containing the names of the symbolic links to be translated.

Keywords

ALLOW_NONEXISTENT

Set this keyword to return an empty string rather than throwing an error if Path contains a non-existent file.

ALLOW_NONSYMLINK

Set this keyword to return an empty string rather than throwing an error if Path contains a path to a file that is not a symbolic link.

NOEXPAND_PATH

Set this keyword to cause FILE_READLINK to use Path exactly as specified, without expanding any wildcard characters or environment variable names included in the path. See FILE_SEARCH for details on path expansion.

Version History

5.6

Introduced

See Also

FILE_LINK, General File Access