# Best way to test for file existence?

**URL:** <https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518>\
**Category:** Ruby\
**Created:** [April 11, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518 "2006-04-11T17:57:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 11, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518/1 "2006-04-11T17:57:02Z")

</div>

All,

What is the best way to determine the existence of a file (more  
specifically, a directory)?

Are there Perl - like file test operators available?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 11, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518/2 "2006-04-11T17:59:20Z")

</div>

File.exist?

Sorry to waste time.

Wes G. wrote:

> All,
> 
> What is the best way to determine the existence of a file (more  
> specifically, a directory)?
> 
> Are there Perl - like file test operators available?
> 
> Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 11, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518/3 "2006-04-11T18:08:30Z")

</div>

Wes G. wrote:

> File.exist?
> 
> Sorry to waste time.

I was so excited to finally see a question I could answer! That raised  
my spirits. Definitely not a waste of my time!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 11, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518/4 "2006-04-11T19:53:04Z")

</div>

Wes G. wrote:

> All,
> 
> What is the best way to determine the existence of a file (more  
> specifically, a directory)?
> 
> Are there Perl - like file test operators available?
> 
> Wes

## $ ri test | cat ------------------------------------------------------------ Kernel#test test(int\_cmd, file1 [, file2] ) =\> obj

```
  Uses the integer <em>aCmd</em> to perform various tests on
  <em>file1</em> (first table below) or on <em>file1</em> and
  <em>file2</em> (second table).

  File tests on a single file:

    Test Returns Meaning
     ?A | Time | Last access time for file1
     ?b | boolean | True if file1 is a block device
     ?c | boolean | True if file1 is a character device

```

…

and so on…

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 11, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/best-way-to-test-for-file-existence/57518/5 "2006-04-11T18:30:13Z")

</div>

On 4/11/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> File.exist?
> 
> Sorry to waste time.
> 
> Wes G. wrote:
> 
> > All,
> > 
> > What is the best way to determine the existence of a file (more  
> > specifically, a directory)?

And since you’re looking for directories, there’s also:  
File.directory?
