{"id":40,"date":"2007-07-26T13:46:59","date_gmt":"2007-07-26T03:46:59","guid":{"rendered":"http:\/\/wonkothesane.com\/blog\/?p=40"},"modified":"2007-07-26T15:33:28","modified_gmt":"2007-07-26T05:33:28","slug":"ruby-rename","status":"publish","type":"post","link":"https:\/\/wonkothesane.com\/blog\/2007\/07\/ruby-rename\/","title":{"rendered":"Ruby Rename"},"content":{"rendered":"<p>You can write a &#8220;better renamer&#8221; in any scripting language, but I&#8217;m learning Ruby, so I thought i&#8217;d give it a go. I started with this basic one-liner I got from a friend<\/p>\n<p><code>alias rubyrename='ruby -e \"a = ARGV.shift; b = ARGV.shift; ARGV.each{ |f| File.rename(f, f.gsub(a, b)) } \"'<\/code><\/p>\n<p>I wanted to add some user friendliness for when i forget what the argument syntax is, so I modified it and made it into a normal script, &#8220;rubyrename.rb&#8221;<\/p>\n<pre>\r\n#!\/opt\/local\/bin\/ruby\r\nunless ARGV.size &gt; 2\r\n  puts \"Usage: rubyrename STRING1 STRING2 REGEXP\"\r\n  puts \"       where STRING1 is to be replace with STRING2 in \"\r\n  puts \"       all filenames that match REGEXP\"\r\n  exit\r\nend\r\na = ARGV.shift\r\nb = ARGV.shift\r\nARGV.each do |f|\r\n  unless f == f.gsub(a, b)\r\n    File.rename(f, f.gsub(a, b))\r\n    puts \"#{f} -&gt; #{f.gsub(a, b)}\"\r\n  end\r\nend<\/pre>\n<p>I also added some bells and whistles<\/p>\n<p>Example:<\/p>\n<pre>\r\nartemis:~\/temp daniel$ ls\r\nfoofoofoo.01    foofoofoo.02    foofoofoo.03<\/pre>\n<pre>artemis:~\/temp daniel$ rubyrename.rb foo bar *\r\nfoofoofoo.01 -&gt; barbarbar.01\r\nfoofoofoo.02 -&gt; barbarbar.02\r\nfoofoofoo.03 -&gt; barbarbar.03\r\n\r\nartemis:~\/temp daniel$ rubyrename.rb bar foobar *\r\nbarbarbar.01 -&gt; foobarfoobarfoobar.01\r\nbarbarbar.02 -&gt; foobarfoobarfoobar.02\r\nbarbarbar.03 -&gt; foobarfoobarfoobar.03\r\n\r\nartemis:~\/temp daniel$ ls\r\nfoobarfoobarfoobar.01   foobarfoobarfoobar.02   foobarfoobarfoobar.03<\/pre>\n<p>Now all I have to do is alias a shorter command to my rubyrename script in my .profile<\/p>\n<pre>\r\nalias rrn=rubyrename.rb\r\n<\/pre>\n<p>Of course <em>rubyrename.rb<\/em> needs to be in my path, or I could have specified the full path in the alias<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can write a &#8220;better renamer&#8221; in any scripting language, but I&#8217;m learning Ruby, so I thought i&#8217;d give it a go. I started with this basic one-liner I got from a friend alias rubyrename=&#8217;ruby -e &#8220;a = ARGV.shift; b = ARGV.shift; ARGV.each{ |f| File.rename(f, f.gsub(a, b)) } &#8220;&#8216; I wanted to add some user [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,4],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-languages","category-rubyrails"],"_links":{"self":[{"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonkothesane.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}