As a beginner to the Ruby language, I often found myself needing feedback listing which files are in a given directory. Ruby, for that reason alone, has been a quick savior. A couple of Google searches on this topic will land you somewhere near the Ruby class Dir. Dir is a part of the standard Ruby distribution, and offers many unique ways of dealing with directories, and the files they hold. You can find out more on the specifics here.
So, my very basic problem was this: I want to search the current directory, and list all the files and folders in the current directory recursively. If you are familiar with bash shell scripting, you’ll know that this task is pretty basic and can be completed with the following:
#!/bin/bash for i in $(ls -R ./) do echo "$i" done
So, why Ruby then? Well, bash doesn’t have all the wonderful pieces of Object Oriented goodness that Ruby does, and most specifically I couldn’t easily tell Bash to spit everything out in an XML file. Besides, this was good practice preparing me for doing production level scripting. To get started, you will need Ruby, so if you are on Ubuntu your life is pretty easy. Just run this command in a terminal:
~$ sudo apt-get install ruby
Now, create a file called listing.rb and put the following in:
Dir.chdir("./")
for i in Dir['**/**']
puts i
end
You can run this script by doing the following:
~$ ruby listing.rb
This should work exactly as the bash script above did. So whats so great? Lets go on adding a method call that accepts a directory, and allow a user to input the directory they wish to list. Modify your listing.rb file to look like this:
def search(dir)
Dir.chdir(dir)
for i in Dir['**/**']
puts i
end
end
#Start of the script
dir = ARGV.shift
search(dir)
[smartads]
If you are unfamiliar with how Ruby handles command line input, the ARGV variable is an array that stores all the arguments a user can specify. Since this is a Ruby array, the shift method will cause the first argument to be popped off and stored in the variable dir. Moving along, let’s see the output this produces. Remember now that this script requires an input directory to be given. To run it you can enter the following:
~$ ruby listing.rb ./
You should see, again, output that shows at least your listing.rb file, and one entry for every other file in the current directory. In both forms of the script I used the Dir[**/**]. The **/** says to match all files and all folders, so if a directory exists in the folder I specified the script will proceed to list the files and folders in that directory. To get an idea of what I mean, try running this command:
~$ ruby listing.rb /etc
Now that we can list a ton the files, how should we store them? That is a bit more of an intermediate topic for another day. For now we can muck around with the Dir class to limit our output. Try changing the **/** to something else, or using wildcards to match something specific in your folder. Try it out, and let us know what you think!


25. January 2009 at 1:51 pm
Be reentrant:
Dir.chdir(dir) do
(code)
end
That way, if you get two relative paths in a row, you don’t end up in the wrong place.
26. January 2009 at 1:56 pm
Dir.glob(“#{dir}/**/**”).each {|f| puts f}