Testing Symlinks

I

Intransition

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.
 
G

Gary Wright

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)}"
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top