Beginners Guide to Quick File Listing Using Ruby

Sun, Jan 25, 2009 (Programming)

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!

Share This on Your Favorite Social Network:
  • Facebook
  • MySpace
  • Twitter
  • Digg
  • StumbleUpon
  • LinkedIn
  • Reddit
  • FriendFeed
  • Tumblr
  • Suggest to Techmeme via Twitter
  • Technorati
  • Mixx
  • Propeller
  • Fark
  • Slashdot
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Print
, , , ,

This post was written by:

Ray Gomez - who has written 46 posts on kallasoft.

Ray, a Linux and Unix nut, spends a majority of his daily ritual programming and testing for Big Blue. In his free time he manages to tweak the currently running thinkpad+KDE4 (WHOA) setup, read, and he occasionally gets out of the fluorescent lights to play roller hockey.

Contact the author

2 Responses to “Beginners Guide to Quick File Listing Using Ruby”

  1. J D Says:

    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.

    Reply

  2. Daniel Haran Says:

    Dir.glob(“#{dir}/**/**”).each {|f| puts f}

    Reply

Leave a Reply