How to get the target of a symbolic link?

I have a line containing the file system path to an existing symbolic link. I want to get the path that this link points to.

Basically, I want me to go through this bit of a hacker:

s = "path/to/existing/symlink" `ls -ld #{s}`.scan(/-> (.+)/).flatten.last 

but I want to do this without going around.

+43
ruby symlink
Aug 6 '09 at 9:45
source share
3 answers

I think readlink is what you are looking for:

 File.readlink("path/to/symlink") 
+59
Aug 6 '09 at 9:55
source share
— -
 require 'pathname' Pathname.new("symlink").realpath 

or readlink as others have said

+43
Aug 06 '09 at 9:56
source share

Or you can try:

 File.realpath("symlink_path") 

Works for both symbolic links and regular files.

+10
Aug 14 '14 at 14:13
source share



All Articles