# Listing folder contents

**URL:** <https://www.ruby-forum.com/t/listing-folder-contents/131349>\
**Category:** Ruby\
**Created:** [March 8, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/listing-folder-contents/131349 "2008-03-08T17:14:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eoghan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ac91a4/32.png) [@eoghan](https://www.ruby-forum.com/u/eoghan)\
**Post date:** [March 8, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/listing-folder-contents/131349/1 "2008-03-08T17:14:40Z")

</div>

Hi  
Im listing the contents of a folder using this:  
Find.find(’/Volumes/MYBOOK/stuff/’) do |f| p f end  
This is fine, but im seeing hidden files are with .\_ prefix.  
Is there any easy way to exclude this?  
The folder contains sub folders alphabetised, as in a, b, c, d etc  
another thing; when i print this i get:  
“/Volumes/MYBOOK/stuff/a/file.stuff”  
how could i just print  
/a/file.stuff?  
Thanks!  
Eoghan

---

<div class="post-metadata">

**Author:** ![eoghan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ac91a4/32.png) [@eoghan](https://www.ruby-forum.com/u/eoghan)\
**Post date:** [April 12, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/listing-folder-contents/131349/2 "2008-04-12T17:02:41Z")

</div>

Hi Eoghan,

On 08.03.2008, at 17:13, eoghan wrote:

> Im listing the contents of a folder using this:  
> Find.find(’/Volumes/MYBOOK/stuff/’) do |f| p f end  
> This is fine, but im seeing hidden files are with .\_ prefix.  
> Is there any easy way to exclude this?

If you just don’t want ‘.\_’ prefix then match the File.basename with  
regex and  
do an if.

Find.find(’/Volumes/MYBOOK/stuff/’) do |f|  
puts(f) if not File.basename(f).match(/^.\_/)  
end

Find.find(`pwd`.chomp) do |f|  
puts(f) if not f.match(/^.\_/)  
end

If you don’t want to see all hidden files you can use Rake::FileList  
instead  
of using Find.find.  
irb\> Rake::FileList[’\*\*/\*’]  
=\> [“bfs\_listing.rb”, “bla”, “bla/bla”, “bla/bla/bla”, “bla/bla/bla/  
bla”,  
“bla/bla/bla/bla/fasel2”, “bla/fasel”, “test1”]

Here my example dir tested with your find  
irb\> Find.find(".") do |f| p f end  
“.”  
“./test1”  
“./bla”  
“./bla/fasel”  
“./bla/bla”  
“./bla/bla/bla”  
“./bla/bla/bla/bla”  
“./bla/bla/bla/bla/fasel2”  
“./bla/bla/bla/bla/.\_nein”  
“./bla/bla/bla/.\_nein”  
“./bla/bla/.\_nein”  
“./bla/.\_nein”  
“./bfs\_listing.rb”  
“./.test”  
“./.\_nein”  
=\> nil

> The folder contains sub folders alphabetised, as in a, b, c, d etc  
> another thing; when i print this i get:  
> “/Volumes/MYBOOK/stuff/a/file.stuff”  
> how could i just print  
> /a/file.stuff?

The behaviour of Rake::FileList is that what I think you want, but  
you can  
implement your own tree traverse if you want.

hth. sz
