[CLUE-Tech] Ruby oneliner (Was: removing spaces from filenames)

Dale K. Hawkins dhawkins at cdrgts.com
Tue May 18 08:27:46 MDT 2004


It just so happens that I ran into the xargs space problem this morning.
Plus, I could not think of the right shell utilities which could help
me.  So, I grabbed my favorite programming language (currently ruby) and
whipped out a script that did the job quick and easy; no messing with
escape characters, etc.

I had a partial mirror of a directory tree on computer "target", the
original tree lives on computer "source", and I wanted to copy the
remaining files (some 1.5 gigs or so) via my laptop.  After having
created a list of the existing trees on both the source and target, I
wrote a ruby script to get the set difference.  (I then used the
resulting difference with fgrep and feed that to rsync to copy the tree
to my laptop).

Note:

1)  I could not use rsync because of the third computer.

2)  I am not necessarily pushing ruby over all languages (though I
hardily recommend it -- take a look at the pick-axe book
http://www.ruby-doc.org/docs/ProgrammingRuby ).  My point is that
sometime (often) it is easier to grab a better tool than to fight with
the shell to the point of frustration.

Anyhow, the script in its entirety:

==============================================================
 #!/usr/bin/ruby

require 'fileutils'

superset = File.open("source-list").collect { | fn | File.basename(fn.chomp) }
subset = File.open("target-list").collect { | fn | File.basename(fn.chomp) }

songs_to_copy = superset - subset

puts songs_to_copy
==============================================================

or as the promised one-liner:

ruby -rfileutils -e 'puts File.open("source-list").collect { | fn | File.basename(fn.chomp) } - File.open("target-list").collect { | fn | File.basename(fn.chomp) }'





More information about the clue-tech mailing list