{"id":85,"date":"2007-06-17T15:25:04","date_gmt":"2007-06-17T21:25:04","guid":{"rendered":"https:\/\/geekmuse.dreamhosters.com\/wp\/?p=85"},"modified":"2007-06-17T16:31:23","modified_gmt":"2007-06-17T22:31:23","slug":"ep56-follow-up-scripts","status":"publish","type":"post","link":"https:\/\/geekmuse.dreamhosters.com\/wp\/?p=85","title":{"rendered":"Ep.#56 Follow-up: Scripts"},"content":{"rendered":"<p>Here&#8217;s the &#8216;rename.pl&#8217; script I mentioned in Ep. #56, originally written my Tom Christiansen and Nathan Torkington in &#8220;The Perl Cookbook&#8221; (Chapter 9.9, Renaming Files, O&#8217;Reilly, 1999)<\/p>\n<blockquote><p><span style=\"font-family:monospace;font-size:9pt;\"><PRE>#!\/usr\/local\/bin\/perl<\/p>\n<p># Usage: rename perlexpr [files]<\/p>\n<p>($op = shift) || die &#8220;Usage: rename perlexpr [filenames]\\n&#8221;;<\/p>\n<p>if (!@ARGV) {<br \/>\n    @ARGV = ;<br \/>\n    chop(@ARGV);<br \/>\n}<br \/>\nfor (@ARGV) {<br \/>\n    $was = $_;<br \/>\n    eval $op;<br \/>\n    next if (-e $_);<br \/>\n    die $@ if $@;<br \/>\n    rename($was,$_) unless $was eq $_;<br \/>\n}<\/PRE><\/span><\/p><\/blockquote>\n<p>Note: I added the 3rd line in the for loop, &#8220;next if (-e $_);&#8221; to prevent clobbering already existing files.<\/p>\n<p>Some common cases where I use this:<\/p>\n<p><UL><br \/>\nLowercase all filenames:<br \/>\n<UL><LI><span style=\"font-size: 12px; font-family: monospace;\">rename.pl &#8216;tr\/A-Z\/a-z\/&#8217; *<\/span><\/UL><\/p>\n<p>Change spaces to underscores:<br \/>\n<UL><LI><span style=\"font-size: 12px; font-family: monospace;\">rename.pl &#8216;s\/\\s\/_\/g&#8217; *<\/span><\/UL><\/p>\n<p>Change JPEG to jpg:<br \/>\n<UL><LI><span style=\"font-size: 12px; font-family: monospace;\">rename.pl &#8216;s\/\\.jpeg$\/\\.jpg\/i&#8217; *.jpeg<\/span><\/UL><\/p>\n<p>Zero pad numbers in filenames (ie. &#8216;blah_1.jpg&#8217; to &#8216;blah_01.jpg&#8217;):<br \/>\n<UL><LI><span style=\"font-size: 12px; font-family: monospace;\">rename.pl &#8216;s\/_(\\d)\\.\/_$1\\.\/&#8217; *<\/span><\/UL><\/p>\n<p>Clean up pesky downloads:<br \/>\n<UL><LI><span style=\"font-size: 12px; font-family: monospace;\">rename.pl &#8216;s\/.xvid\/\/i&#8217; *<br \/>\n<LI>rename.pl &#8216;s\/.hdvt\/\/i&#8217; *<\/span><\/UL><\/p>\n<p>Clean up filenames:<br \/>\n<UL><LI><span style=\"font-size: 12px; font-family: monospace;\">rename.pl &#8220;s\/\\&#8217;\/\/&#8221; *<br \/>\n<LI>rename.pl &#8216;s\/\\&#8221;\/\/&#8217; *<br \/>\n<LI>rename.pl &#8216;s\/[\\s,_]\/\/&#8217; *<\/span><\/UL><br \/>\n<\/UL><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the &#8216;rename.pl&#8217; script I mentioned in Ep. #56, originally written my Tom Christiansen and Nathan Torkington in &#8220;The Perl Cookbook&#8221; (Chapter 9.9, Renaming Files, O&#8217;Reilly, 1999) #!\/usr\/local\/bin\/perl # Usage: rename perlexpr [files] ($op = shift) || die &#8220;Usage: rename perlexpr [filenames]\\n&#8221;; if (!@ARGV) { @ARGV = ; chop(@ARGV); } for (@ARGV) { $was = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":0,"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmuse.dreamhosters.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}