{"id":109,"date":"2009-09-15T13:36:09","date_gmt":"2009-09-15T21:36:09","guid":{"rendered":"https:\/\/digitalcardboard.com\/blog\/?p=109"},"modified":"2012-12-11T11:31:14","modified_gmt":"2012-12-11T19:31:14","slug":"linux-rename-utility-on-os-x","status":"publish","type":"post","link":"https:\/\/digitalcardboard.com\/blog\/2009\/09\/15\/linux-rename-utility-on-os-x\/","title":{"rendered":"Linux &#8216;rename&#8217; utility on OS X"},"content":{"rendered":"<p><em>(edit 12\/11\/2012 to add <a href=\"#homebrew\">homebrew alternative<\/a>)<\/em><\/p>\n<p>So you you want the powerful Perl-based <strong>rename<\/strong> utility that comes with many Linux and *BSD distributions, but sadly not on Apple&#8217;s mighty OS X? Well so did I. <em>(Go away you fanboys of all types. I hear not your rants about which operating system is for the win)<\/em><\/p>\n<p>Thinking about it for a moment, I realize there&#8217;s nothing really stopping me from just taking the script, and thankfully it&#8217;s free software and we can pretty much do whatever we need to with it.<\/p>\n<p>The <strong>rename<\/strong> utility is tucked away under <strong>\/usr\/bin\/<\/strong> on my Ubuntu machine.\u00a0 To get it to your OS X machine, it&#8217;s just a matter of copying <strong>\/usr\/bin\/rename<\/strong> on a Linux box and dropping it in <strong>\/usr\/local\/bin\/<\/strong> or somewhere else that&#8217;s in your PATH on your OS X box.\u00a0 If you&#8217;re really feeling the need for the man page, you can grab it from <strong>\/usr\/share\/man\/man1\/rename.1.gz<\/strong>.\u00a0 I stuck the man page in <strong>\/usr\/local\/share\/man\/man1\/<\/strong>.<\/p>\n<p>And that&#8217;s it! You can now go about using Perl regular expressions to rename multiple files and get lost in everything that is regexp.<\/p>\n<p>Don&#8217;t have access to a Linux machine, you say? Oh fine. Here&#8217;s what I pulled from my Ubuntu 9.04 distro:<\/p>\n<ul>\n<li><a href=\"https:\/\/digitalcardboard.com\/blog\/wp-content\/uploads\/2009\/09\/rename\" target=\"_blank\">rename<\/a><\/li>\n<li><a href=\"https:\/\/digitalcardboard.com\/blog\/wp-content\/uploads\/2009\/09\/rename.1.gz\" target=\"_blank\">rename man page (rename.1.gz)<\/a><\/li>\n<\/ul>\n<p>If you download the <strong>rename<\/strong> utility from here, you&#8217;ll probably need to run <strong>&#8216;chmod a+x&#8217;<\/strong> to give execute permissions to the file after copying it to the appropriate place but otherwise things should be the same as above.<\/p>\n<h3>Examples<\/h3>\n<p>Wondering how it works?\u00a0 Say you&#8217;d like to rename a bunch of <strong>.wav<\/strong> files and prepend the phrase <em>&#8216;iheartrobots&#8217;<\/em> to the front of each filename (practical, I know).\u00a0 Well you&#8217;d type the following:<\/p>\n<blockquote>\n<pre>rename -v 's\/^\/iheartrobots\/' *.wav<\/pre>\n<\/blockquote>\n<p>The <strong>&#8216;-v&#8217;<\/strong> parameter prints the names of the files that are being renamed.\u00a0 Helpful and I use it most of the time.<\/p>\n<p>Or how about replacing all instances of the word <em>&#8216;cat&#8217;<\/em> with the word <em>&#8216;dog&#8217;<\/em> in the filename of all <strong>.rtf<\/strong> files in a folder? Bam.<\/p>\n<blockquote>\n<pre>rename -v 's\/cat\/dog\/i' *.wav<\/pre>\n<\/blockquote>\n<p>The <strong>&#8216;i&#8217;<\/strong> at the end of the regular expression makes the search case-insensitive.<\/p>\n<p>Finally, say you had a bunch of files that looked like this: <em>Waltzing.With.Famous.People.S01E09.HDTV.XviD-SYS.[bztv].avi<\/em><\/p>\n<p>And you wanted to get rid of everything between <em>S01E09<\/em> and <em>.avi<\/em>, you could use the following:<\/p>\n<blockquote>\n<pre>rename -v 's\/HDTV.*avi\/avi\/i' *.avi<\/pre>\n<\/blockquote>\n<p><a name=\"homebrew\" id=\"homebrew\"><\/a><\/p>\n<h3>The Homebrew Alternative<\/h3>\n<p>Now, thanks to <a href=\"http:\/\/mxcl.github.com\/homebrew\/\" target=\"_blank\">Homebrew<\/a>, you can skip all this nonsense and just do:<\/p>\n<blockquote>\n<pre>brew install rename<\/pre>\n<\/blockquote>\n<p>If you&#8217;re not using <a href=\"http:\/\/mxcl.github.com\/homebrew\/\" target=\"_blank\">Homebrew<\/a>, you should be.<\/p>\n<h3>More Information<\/h3>\n<p>If you&#8217;d like to get more information on the <strong>rename<\/strong> utility described here, the most obvious place is the man page (<strong>man rename<\/strong>) or online <a href=\"http:\/\/unixhelp.ed.ac.uk\/CGI\/man-cgi?rename\" target=\"_blank\">here<\/a>.<\/p>\n<p>Additionally, since it&#8217;s a Perl utility, more information on the Perl regular expression pattern-matching language can be found <a href=\"http:\/\/perldoc.perl.org\/perlretut.html\" target=\"_blank\">straight from the horse&#8217;s mouth<\/a>.\u00a0 Regular expressions are a madness unto themselves, but once you get the hang of it, most powerful stuff.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(edit 12\/11\/2012 to add homebrew alternative) So you you want the powerful Perl-based rename utility that comes with many Linux and *BSD distributions, but sadly not on Apple&#8217;s mighty OS X? Well so did I. (Go away you fanboys of all types. I hear not your rants about which operating system is for the win) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8],"tags":[23,22,124],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-software","tag-linux","tag-osx","tag-software"],"_links":{"self":[{"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":5,"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalcardboard.com\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}