# Test if Directory is Empty

**URL:** <https://www.ruby-forum.com/t/test-if-directory-is-empty/72320>\
**Category:** Ruby\
**Created:** [October 16, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320 "2006-10-16T12:42:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/1 "2006-10-16T12:42:12Z")

</div>

Hi all

I browsed through the FileUtils and Dir documentation and found no  
obvious way to test if a directory exists and is empty. Various hacks  
can be used (like checking file count and size, testing if the  
directory can be deleted…) but they all left me wondering if i some  
how missed a Dir.isEmpty? like method ?

So in short is there a real Ruby way of telling whether a directory if  
empty

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/2 "2006-10-16T12:52:13Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi all
> 
> I browsed through the FileUtils and Dir documentation and found no  
> obvious way to test if a directory exists and is empty. Various hacks  
> can be used (like checking file count and size, testing if the  
> directory can be deleted…) but they all left me wondering if i some  
> how missed a Dir.isEmpty? like method ?
> 
> So in short is there a real Ruby way of telling whether a directory if  
> empty

if Dir["/foo/bar/\*"].empty?

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/3 "2006-10-16T16:21:46Z")

</div>

On Mon, 16 Oct 2006, Daniel B. wrote:

> > empty
> 
> if Dir["/foo/bar/\*"].empty?

that’ll crawl if a directory is huge though… i’ve used this

d = “the\_dir”

empty =  
catch(“empty”){  
Dir.glob("#{ d }/_"){ throw “empty”, false } # if we find any  
file return false  
Dir.glob("#{ d }/._"){ throw “empty”, false } # if we find any  
dotfile/hidden return false  
throw “empty”, true # otherwise return  
true  
}

which someone will probably compact… note that it returns false when  
the  
first file/dotfile is found.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/4 "2006-10-16T18:28:50Z")

</div>

Hi,

At Mon, 16 Oct 2006 23:19:30 +0900,  
removed\_email\_address@domain.invalid wrote in [ruby-talk:220018]:

> which someone will probably compact… note that it returns false when the  
> first file/dotfile is found.

!Dir.foreach(dirname) {|n| break true unless /\A..?\z/ =~ n}  
!Dir.enum\_for(:foreach, dirname).any? {|n| /\A..?\z/ !~ n}

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/5 "2006-10-16T18:45:06Z")

</div>

On Tue, 17 Oct 2006, Nobuyoshi N. wrote:

> Hi,
> 
> At Mon, 16 Oct 2006 23:19:30 +0900,  
> removed\_email\_address@domain.invalid wrote in [ruby-talk:220018]:
> 
> > which someone will probably compact… note that it returns false when the  
> > first file/dotfile is found.
> 
> !Dir.foreach(dirname) {|n| break true unless /\A..?\z/ =~ n}  
> !Dir.enum\_for(:foreach, dirname).any? {|n| /\A..?\z/ !~ n}

nice. double negation is always good!

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/6 "2006-10-16T18:53:07Z")

</div>

Hi,

Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> At Mon, 16 Oct 2006 23:19:30 +0900,  
> removed\_email\_address@domain.invalid wrote in [ruby-talk:220018]:
> 
> > which someone will probably compact… note that it returns false when the  
> > first file/dotfile is found.
> 
> !Dir.foreach(dirname) {|n| break true unless /\A..?\z/ =~ n}  
> !Dir.enum\_for(:foreach, dirname).any? {|n| /\A..?\z/ !~ n}

Dir.entries(dirname).join == “…”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/7 "2006-10-16T19:07:14Z")

</div>

removed\_email\_address@domain.invalid wrote:

> –  
> Lars R.

You could try:

class Dir  
def Dir.isEmpty?(path)  
Dir.entries(path) == [".", “…”]  
end  
end

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/8 "2006-10-16T21:22:09Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > >
> 
> empty =
> 
> cheers.
> 
> -a

Not sure about using throw to return values there… Also, won’t the  
“Dir.glob(”#{ d }/.\*")" return [".", “…”] (well, pass “.” to the block)  
and break this?

How about:

def isEmpty?(dir)  
Dir.glob("#{dir}/\*", File::FNM\_DOTMATCH) do |f|  
return false unless f =~ /..?/  
end  
return true  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/9 "2006-10-16T19:21:16Z")

</div>

WATANABE Hirofumi wrote:

> Dir.entries(dirname).join == “…”

Dir.entries(dirname).size == 2

But this still has the disadvantage that Ara mentioned for huge dirs…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 17, 2006, 5:57am UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/10 "2006-10-17T05:57:02Z")

</div>

On Tue, 17 Oct 2006, John T. wrote:

not sure what you mean there - that’s the correct usage… read the ri  
for  
catch.

> end  
> yes. that’s definitely better…

that regex is deadly though:

harp:~ \> irb  
irb(main):001:0\> “this is a file …” =~ /…?/ ? true : false  
=\> true

refining some of the ideas in the thread:

```
 harp:~ > cat a.rb
 class Dir
   def empty?
     Dir.glob("#{ path }/*", File::FNM_DOTMATCH) do |e|
       return false unless %w( . .. ).include?(File::basename(e))
     end
     return true
   end
   def self.empty? path
     new(path).empty?
   end
 end

 d = Dir.new '/tmp'
 p d.empty?

 require 'fileutils'
 FileUtils.rm_f 'empty'
 FileUtils.mkdir_p 'empty'
 p(Dir.empty?('empty'))

 harp:~ > ruby a.rb
 false
 true

```

now we can begin the discussion on how to use such a method - since  
nearly any  
code that did would have a race condition! 😉

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 17, 2006, 12:42am UTC](https://www.ruby-forum.com/t/test-if-directory-is-empty/72320/11 "2006-10-17T00:42:01Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > > So in short is there a real Ruby way of telling whether a directory if  
> > > empty
> > 
> > if Dir["/foo/bar/\*"].empty?
> 
> that’ll crawl if a directory is huge though

True. Oh, look - Windows has a PathIsDirectoryEmpty() function. 🙂

win32-dir 0.3.1 coming soon…

Thanks,

Dan
