in reply to Help on renaming a bunch of files
The book also includes some examples of using the script.#!/usr/local/bin/perl # Usage: rename <perlexpr> [files] ($op = shift) || die "Usage: $0 <perlexpr> [files]\n"; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; }
Rename files matching *.bak to strip the extension: rename 's/\.bak$//' *.abc Add the extensions back on:
Translate uppercase names to lower: rename 'tr/A-Z/a-z/' * And more:rename '$_ .= ".bak"' * rename 's/$/.bak/' *
It was sad that the later editions of Programming Perl didn't include the sample scripts, but fortunately the Perl CookBook has since filled that need.rename 's/foo/bar; $_ = $was if -e' *foo* find . -print | rename 's/readme/README/i' find . -print | rename 's/$/.old/ if -M $_ > 0.5' find . -name '*,v' -print | \ rename 's#(.*)/#$1/RCS#, $x{$1}++ || mkdir("$1/RCS", 0777)'
|
|---|