# HowTo get ruby to report a sumbolic link?

**URL:** <https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043>\
**Category:** Ruby\
**Created:** [July 22, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043 "2010-07-22T22:08:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 22, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043/1 "2010-07-22T22:08:37Z")

</div>

I am creating a series of symbolic links to target directories. I wish  
to check to see if the symbolic link exists or not. When I do this  
(using rspec):

File.stat(@soft\_link\_target).ftype.should == ‘link’

I get this:

```
  expected: "link",
       got: "directory" (using ==)

```

Which tells me that the link is there but it is reporting the target  
directory and not the link itself. How do I tell if the thing that I am  
checking is a symbolic link to a directory and not the directory itself?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 22, 2010, 10:16pm UTC](https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043/2 "2010-07-22T22:16:40Z")

</div>

On Thu, Jul 22, 2010 at 1:08 PM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Which tells me that the link is there but it is reporting the target  
> directory and not the link itself. How do I tell if the thing that I am  
> checking is a symbolic link to a directory and not the directory itself?

ri File::Stat tells me that there’s a #symlink? method you could use:

File.stat( @soft\_link\_target ).should be\_symlink

… I believe that’s the right syntax for rspec, but you get the idea.

Ben

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 23, 2010, 12:06am UTC](https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043/3 "2010-07-23T00:06:09Z")

</div>

On Thu, Jul 22, 2010 at 2:26 PM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> As File::stat automatically follows symbolic links, symlink? will always  
> be false for an object returned by File::stat.

You’re right. Sorry, jumped the gun. There’s also File.symlink?,  
which does work correctly.

Ben

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 22, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043/4 "2010-07-22T23:26:10Z")

</div>

Ben B. wrote:

> ri File::Stat tells me that there’s a #symlink? method you could use:
> 
> File.stat( @soft\_link\_target ).should be\_symlink
> 
> … I believe that’s the right syntax for rspec, but you get the idea.

rdoc also says:

As File::stat automatically follows symbolic links, symlink? will always  
be false for an object returned by File::stat.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 23, 2010, 12:33am UTC](https://www.ruby-forum.com/t/howto-get-ruby-to-report-a-sumbolic-link/192043/5 "2010-07-23T00:33:59Z")

</div>

Ben B. wrote:

> You’re right. Sorry, jumped the gun. There’s also File.symlink?,  
> which does work correctly.

Yes, it does. Which surprises me since that is what I started with. I  
suppose something else must have been wrong in the test and I simply  
misdiagnosed the reason.

Thanks.
