rename v. script

Steven W. Orr steveo at syslang.net
Thu May 19 14:01:45 UTC 2005


On Thursday, May 19th 2005 at 06:01 +0100, quoth THUFIR HAWAT:

=>given:
=>
=>bash-3.00$ ls -al
=>total 984
=>drwxr-xr-x   2 thufir thufir  4096 May 16 02:18 .
=>drwx------  15 thufir thufir  4096 May 16 02:18 ..
=>-rw-r--r--   1 thufir thufir 12354 May 14 22:30 1151154
=>-rw-r--r--   1 thufir thufir 12955 May 14 22:16 1199229
=>-rw-r--r--   1 thufir thufir 13639 May 14 22:22 1238181
=>-rw-r--r--   1 thufir thufir 13996 May 14 22:24 1268816
=>-rw-r--r--   1 thufir thufir 12176 May 14 22:37 1276032
=>...
=>
=>and then:
=>
=>bash-3.00$ mv 1151154 1151154.html
=>bash-3.00$ mv 1199229 1199229.html
=>bash-3.00$ mv 1238181 1238181.html
=>
=>each file can be changed.  however, there are many files and that is
=>very tedious.  can the above three commands (or 50, or 100) be written
=>with a single rename command?
=>
=>thanks,

My preference is the following rename script from the Camel book.

#!/usr/bin/perl
$op = shift;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}


Now you can use perl regex to get your renames accomplished.

rename 's/$/.html/' *

But you're not limited to simple substitutions. e.g., Let's flip all the 
characters to lowercase.

rename 'tr/A-Z/a-z/' *

Enjoy.

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net




More information about the users mailing list