Ruby Command Line Scripting

Have you ever heard of a Perl script being run on a text file to edit it? Or maybe a Perl script that edits multiple files quickly in an otherwise tedious task? The reason Perl has been used for this task is because it can be run straight from the command line.

h3. Why Ruby?

What does this have to do with Ruby you ask? Well before I answer that, for those of you who have never heard of the computer language Ruby, be sure to read about it on Wikipedia. Now back to the question; well Ruby could also be run from the command line and is in fact encouraged in most tutorials for Ruby.

Ok, so both Ruby and Perl could be run from the command line, why use Ruby instead of Perl? That is strictly a personal preference (and the fact that I don’t know Perl), but I recommend you to consider using Ruby too. It’s syntax is easy to learn and you end up with easy to read code.

h3. The Problem

Ever since I learned about Ruby through Ruby on Rails I wanted to learn more about it. As anybody in the scientific community would say so, to learn about something, the best thing to do is experiment. To experiment, however, I needed a question or a problem. That I did not have, until yesterday. I was tediously editing Mac OSX filenames into Windows filenames. Every file was messed up and I was just fed up with doing the same thing over and over. Then it hit me, just several hours before I had tried converting a Mac .dmg file into a Windows-readable .iso file. I had failed in doing so, but the point was how I tried to do it. I had found a Perl script that needed to be run in the command line in order to work. At that very moment, another lightbulb lit above my head: Ruby can be run in the command line too! So there is my problem, how to write a Ruby script to do a tedious renaming task.

h3. The Solution

Once I found something as exciting as programming something that I actually needed, I was just estatic! I was lucky that just a day before I had been trying to convert my C++ programming assignment into Ruby code. Again I did not succeed yet, but again that was not the point. The point was I was reading the tutorials and references, and I remembered a section on regular expressions. That is useful because, if you didn’t know, regular expressions is a string that matches other strings. This is useful because filenames are basically a string (of letters), and my code has to match it, then rename it.

With all this knowledge, I was still not rolling. The problem was that I didn’t know how to look through a directory for files. So, I googled, and googled. It took a while, but finally I found a Ruby script that was used to search through a directory. It was not long till I put it all together.

Here is the code I used to rename any files with the name ._something (like ._Apple) into something.icns:

puts "hello world\\n"

  Dir["._*"].each do |file|
    unless file =~ /._(\\w|\\s)+.rtf/ || file =~ /._(\\w|\\s)+.webloc/
      puts "#{file} renamed"
      newfile = "._")
      newfile << ".icns"
      File.rename(file, newfile)
      puts "Renamed to #{file}\\n"

  puts "\\n\\n"

Line 1 basically just a little intro I decided to add for no reason at all. Then the next block goes into each file and unless the filename is ._something.rtf or ._something.webloc it will perform the renaming. It will say "filename renamed," do the renaming and then say "Renamed to newname." After it's done, it skipped two lines so it looks nicely on the command line.

The regular expressions are always in between forward slashes (/).

unless file =~ /._(\\w|\\s)+.rtf/ || file =~ /._(\\w|\\s)+.webloc/

This line says if a string matches ._(one or more letters or spaces).rtf or ._(one or more letters or spaces).webloc it will ignore it. The unless means unless this is true do the following, pretty straightforward. The + means one or more of the things inside the parenthesis. The =~ is used to match something to a regular expression. And finally the || means ‘or’ much like in many other computer language.

In order to run this script, I saved it as rename.rb file and then just typed ruby rename.rb on the command line in the directory I wanted it to perform the task.

That concludes my little story about Ruby scripting. If you want to read more about regular expressions in Ruby, there's a great tutorial from Mark Slagell (the tutorial is a modified, translated version of one originally written by Ruby creator Yukihiro "Matz" Matsumoto). And be sure to download Ruby if you wish to use this. There's a handy package with everything, including editors, on the Ruby on Rails download page. (Direct file link)