-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3719d49
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Overview | ||
-------- | ||
|
||
`rename` is a script which renames files according to a perl expression. | ||
|
||
Apparently Larry Wall wrote such a script years ago (with almost identical | ||
syntax to mine! great minds think alike!), but I didn't know that or I wouldn't | ||
have reinvented the wheel. If I do say so, this version's a bit better, and | ||
safer when it comes to clobbering your files. | ||
|
||
Examples | ||
-------- | ||
|
||
% rename 's/\.bak$//' *.bak # strips the .bak off all .bak files | ||
% rename 's/\d+$/$&+1/e' messages.* # incremements numeric suffixes | ||
% rename '$_ .= "-" . time()' log* # adds -seconds-since-epoch to files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/perl | ||
# | ||
# renames files according to a perl expression | ||
# | ||
# 2007-03-13 [email protected] | ||
# | ||
# e.g.: rename 's/\.bak$//' *.bak # strips the .bak off all .bak files | ||
# rename 's/\d+$/$&+1/e' messages.* # incremements numeric suffixes | ||
# rename '$_ .= "-" . time()' log* # adds -seconds-since-epoch to files | ||
# | ||
|
||
use File::Basename; | ||
use Getopt::Std; | ||
use strict; | ||
|
||
## argument handling | ||
my %opt; | ||
getopts('hvnc', \%opt); | ||
my $prog = basename($0); | ||
|
||
die <<EOF if $opt{h} || @ARGV < 2; | ||
usage: $prog [-n | -v] 'perl expr' filename [filename2 [...]] | ||
-v: verbose | ||
-n: dry run; implies -v | ||
-c: confirm; does a dry run, prompts for confirmation, then does for real | ||
EOF | ||
$opt{n} = 1 if $opt{c}; | ||
$opt{v} = 1 if $opt{n}; | ||
|
||
## grab expression | ||
my $expr = shift; | ||
|
||
## pass 1: validate and build hash | ||
my (@items, %exist); | ||
for (@ARGV) { | ||
next unless -e; | ||
push(@items, $_); | ||
$exist{$_} = 1; | ||
} | ||
|
||
## pass 2: rename | ||
my (@tmp, @final); | ||
for (@items) { | ||
## do substitution | ||
my $old = $_; | ||
eval $expr; | ||
my $new = $_; | ||
|
||
if ($new ne $old) { | ||
## find conflicts | ||
if ($exist{$new}) { | ||
my $i = 0; | ||
$i++ while -e "$new.rename.$i"; | ||
push(@tmp, "$new.rename.$i"); | ||
push(@final, $new); | ||
$new = "$new.rename.$i"; | ||
} | ||
|
||
do_rename($old, $new); | ||
} | ||
|
||
delete $exist{$old}; | ||
} | ||
|
||
## pass 3: cleanup | ||
while (@tmp) { | ||
do_rename(shift(@tmp), shift(@final)); | ||
} | ||
|
||
## pass 4: confirm and perform for real | ||
if ($opt{c}) { | ||
$| = 1; | ||
print "Perform renames? [Y/n] "; | ||
my $yn = <STDIN>; | ||
exec($0, $expr, @ARGV) unless $yn =~ /n/i; | ||
} | ||
|
||
sub do_rename { | ||
my ($old, $new) = @_; | ||
print "$old -> $new\n" if $opt{v}; | ||
rename($old, $new) unless $opt{n}; | ||
} |