JAVA how to find the target file path that a symbolic link points to?

12,290

Solution 1

https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#toRealPath-java.nio.file.LinkOption...-

Path toRealPath(LinkOption... options) throws IOException Returns the real path of an existing file. The precise definition of this method is implementation dependent but in general it derives from this path, an absolute path that locates the same file as this path, but with name elements that represent the actual name of the directories and the file. For example, where filename comparisons on a file system are case insensitive then the name elements represent the names in their actual case. Additionally, the resulting path has redundant name elements removed.

If this path is relative then its absolute path is first obtained, as if by invoking the toAbsolutePath method.

The options array may be used to indicate how symbolic links are handled. By default, symbolic links are resolved to their final target. If the option NOFOLLOW_LINKS is present then this method does not resolve symbolic links. Some implementations allow special names such as ".." to refer to the parent directory. When deriving the real path, and a ".." (or equivalent) is preceded by a non-".." name then an implementation will typically cause both names to be removed. When not resolving symbolic links and the preceding name is a symbolic link then the names are only removed if it guaranteed that the resulting path will locate the same file as this path.

Parameters: options - options indicating how symbolic links are handled Returns: an absolute path represent the real path of the file located by this object Throws: IOException - if the file does not exist or an I/O error occurs SecurityException - In the case of the default provider, and a security manager is installed, its checkRead method is invoked to check read access to the file, and where this path is not absolute, its checkPropertyAccess method is invoked to check access to the system property user.dir

Use the toRealPath() method from nio. The NO_FOLLOW_LINKS LinkOption does the opposite of what you are trying to do, so don't use it.

Path realPath = aFile.toPath().toRealPath()

Path realPath = aPath().toRealPath()

One gotcha with this, is that, unlike toAbsolutePath, toRealPath throws an IOException since it actually has to interact with the file system to find the real path.

Solution 2

java.nio.file.Files.readSymbolicLink might be what you need.

Share:
12,290
edjm
Author by

edjm

I've been doing full development professionally since 2000. Started out with HTML & Javascript which lead into learning Coldfusion & SQL. Got into some system administration doing deployments and setting up tape backups. A few years later got back into doing Java (which I had done a little back in college) and started using JSP. Picked up use of Postgresql, MySql, MS Sql server as well as Oracle DB. Learned Flash and Actionscripting. Back to Coldfusion in 2009-2012 and ExtJS. Learned some ETL software and use with Java processing of large data. Jira exposure with Jenkins. Then stuck in Java land for many years with Spring and some horrible hibernate environments. Last couple years have gotten the pleasure of learning Angular. Certified in Google Cloud but never got to do anything with that. Hard to chase new tech when projects have you stuck in old tech.

Updated on June 13, 2022

Comments

  • edjm
    edjm almost 2 years

    I'm trying to find a way in Java to figure out the file path that a symbolic link is pointing to. We have a system that monitors a symbolic link folder, does some stuff based on the file name, deletes that symbolic link file. What I need to do is figure out where each symbolic link file is pointing to so I can delete that file as well when the symbolic link. This is using Java on a RHEL system.

    Any guidance to API topics would be greatly appreciated. I'm hitting a wall on my searches.

  • edjm
    edjm almost 7 years
    Great details and link to the API, additional note that the exception SecurityException can also be thrown. This was exactly the information that I needed. Thanks for your help @Novaterata
  • edjm
    edjm almost 7 years
    Thank you for the import path.
  • Novaterata
    Novaterata almost 7 years
    Right, but IOException is checked so you'll either have to modify your interface or use a try/catch
  • juanmf
    juanmf over 6 years
    beware that it will return a relative path, say /usr/local/link -> ./file for /usr/local/file. Then Files.readSymbolicLink returns file and Paths.get("/usr/local/files").equals( Files.readSymbolicLink(Paths.get("/usr/local/link"))) is false. In this case I used Files.isSameFile(file, link). And to get the absolute path of the target file of a link, link.toRealPath()