# Why does File.exists? not support regexp? It would be very h

**URL:** <https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750>\
**Category:** Ruby\
**Created:** [September 4, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750 "2007-09-04T17:46:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/1 "2007-09-04T17:46:17Z")

</div>

Hi,  
if I wanna check if a file exists like fglrx.i386.8.40.3.ko why is  
File.exists? not able to handle regexp to check this like:

system(‘rm fglrx\*’) if File.exists?(/^fglrx.+/)

–\> ./test.rb:4:in `exists?’: can’t convert Regexp into String  
(TypeError)

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/2 "2007-09-04T17:54:20Z")

</div>

On Sep 4, 9:45 am, kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> if I wanna check if a file exists like fglrx.i386.8.40.3.ko why is File.exists? not able to handle regexp to check this like:
> 
> system(‘rm fglrx\*’) if File.exists?(/^fglrx.+/)
> 
> → ./test.rb:4:in `exists?': can’t convert Regexp into String (TypeError)

Dir[“fglrx\*”].each{ |file| File.delete(file) }

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/3 "2007-09-04T18:02:24Z")

</div>

On Wed, 5 Sep 2007 00:52:57 +0900  
Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dir[“fglrx\*”].each{ |file| File.delete(file) }

that’s pretty much the same like: system(‘rm -f fglrx\*’) but I’m asking  
why one doesn’t allow regexpr into File.exists?

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/4 "2007-09-04T18:28:29Z")

</div>

On Sep 4, 10:00 am, kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, 5 Sep 2007 00:52:57 +0900
> 
> Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Dir[“fglrx\*”].each{ |file| File.delete(file) }
> 
> that’s pretty much the same like: system(‘rm -f fglrx\*’) but I’m asking why one doesn’t allow regexpr into File.exists?

Probably because most people wouldn’t find it intuitive, there’s  
already easy alternatives, and there doesn’t seem to be any desire for  
it among the Ruby community in general. You’re the first person to  
request it that I’ve seen.

> From a development point of view, it’s not only more maintenance, but  
> also a slipperly slope towards having to review every other boolean  
> File method.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/5 "2007-09-04T23:49:41Z")

</div>

Gary W. wrote:

> I would guess it is because file name pattern matching  
> is subtly different from string pattern matching. In  
> particular you have the problem of directory hierarchies  
> and how to handle the separator character (typically ‘/’).  
> The common file pattern syntax is also _not_ the same  
> as general regular expressions.

The slocate(1) program does have a -r option for searching file paths  
with a regex, so there is some precedent.

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 4, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/6 "2007-09-04T23:30:38Z")

</div>

On Sep 4, 2007, at 12:00 PM, kazaam wrote:

> On Wed, 5 Sep 2007 00:52:57 +0900  
> Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Dir[“fglrx\*”].each{ |file| File.delete(file) }
> 
> that’s pretty much the same like: system(‘rm -f fglrx\*’) but I’m  
> asking why one doesn’t allow regexpr into File.exists?

I would guess it is because file name pattern matching  
is subtly different from string pattern matching. In  
particular you have the problem of directory hierarchies  
and how to handle the separator character (typically ‘/’).  
The common file pattern syntax is also _not_ the same  
as general regular expressions.

Gary W.

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 5, 2007, 12:09am UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/7 "2007-09-05T00:09:44Z")

</div>

Joel VanderWerf wrote the following on 04.09.2007 23:47 :

> > I would guess it is because file name pattern matching  
> > is subtly different from string pattern matching. In  
> > particular you have the problem of directory hierarchies  
> > and how to handle the separator character (typically ‘/’).  
> > The common file pattern syntax is also _not_ the same  
> > as general regular expressions.
> 
> The slocate(1) program does have a -r option for searching file paths  
> with a regex, so there is some precedent.

slocate uses it’s own database, not direct file access. File.exists?  
uses a system call directly. Using regexes would need to list all files  
that could match (a directory or the whole virtual filesystem depending  
on what you are matching with: a filename in a given directory or a  
path). This is far from a common need and in the general case far too  
slow: developpers are better off coding the regex matching code  
themselves and optimize it by listing the smallest subset of filepaths  
the OS can give them before they filter with a regex (which is trivial  
to do in Ruby anyway)…

Lionel

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 5, 2007, 12:05am UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/8 "2007-09-05T00:05:43Z")

</div>

On Sep 4, 8:43 am, kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> if I wanna check if a file exists like fglrx.i386.8.40.3.ko why is File.exists? not able to handle regexp to check this like:
> 
> system(‘rm fglrx\*’) if File.exists?(/^fglrx.+/)
> 
> → ./test.rb:4:in `exists?': can’t convert Regexp into String (TypeError)
> 
> –  
> kazaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I agree that file matching via regular expressions is very convenient  
in certain situations that are not handled by globs. You might want to  
look at Rio ([http://rio.rubyforge.org](http://rio.rubyforge.org)).

---

<div class="post-metadata">

**Author:** ![kazaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kazaam](https://www.ruby-forum.com/u/kazaam)\
**Post date:** [September 5, 2007, 12:21am UTC](https://www.ruby-forum.com/t/why-does-file-exists-not-support-regexp-it-would-be-very-h/113750/9 "2007-09-05T00:21:46Z")

</div>

Lionel B. wrote:

> > > I would guess it is because file name pattern matching  
> > > that could match (a directory or the whole virtual filesystem depending  
> > > on what you are matching with: a filename in a given directory or a  
> > > path). This is far from a common need and in the general case far too  
> > > slow: developpers are better off coding the regex matching code  
> > > themselves and optimize it by listing the smallest subset of filepaths  
> > > the OS can give them before they filter with a regex (which is trivial  
> > > to do in Ruby anyway)…

Well, there’s “find -regex”, too, but I agree that regex matching is  
probably better for narrowing down a globbed search.
