Testing Symlinks

I probably should know the answers to these, but it’s never come up
before.

How to check if a symbolic link exists and already points to where you
expect it to point?

And how to check for dead symbolic links?

Thanks.

On Mar 9, 2010, at 1:08 PM, Intransition wrote:

I probably should know the answers to these, but it’s never come up
before.

How to check if a symbolic link exists and already points to where you
expect it to point?

And how to check for dead symbolic links?

Try playing around with the following:

def info(path)
lstat = File::lstat(path) rescue nil
return “not found” unless lstat
if lstat.symlink?
target = File::readlink(path)
stat = File::stat(path) rescue nil
if stat
“symlink to #{target}”
else
“dead symlink to #{target}”
end
else
“not a symlink”
end
end

ARGV.each { |f|
puts “#{f}: #{info(f)}”
}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs