Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a symlink in Java [duplicate]

Tags:

Given a Java 'File' object, how can I detect whether or not it refers to a symlink?

(If it helps/matters, I know the file refers to a directory, not to a file)

like image 829
Matt Sheppard Avatar asked Mar 22 '10 06:03

Matt Sheppard


People also ask

Does rm RF follow symlinks?

No. rm -rf won't follow symbolic links - it will simply remove them.

Does cp preserve symlinks?

As we know, cp copies the symbolic links (symlinks) in the source directory as symlinks to the destination directory.

What happens when you copy a symlink?

Copy symbolic links as files '-L' ,'--dereference' - Follow symbolic links when copying from them. With this option, cp cannot create a symbolic link. For example, a symlink (to regular file) in the source tree will be copied to a regular file in the destination tree.


1 Answers

File.getCanonicalPath() resolves symlinks

A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

I assume you can compare the result of getCanonicalPath() and getAbsolutePath().

Update: It appears this question has already been asked - check the answers there

like image 114
Bozho Avatar answered Oct 31 '22 19:10

Bozho