Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dbushong committed Aug 8, 2010
0 parents commit 3719d49
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
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
82 changes: 82 additions & 0 deletions rename
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};
}

0 comments on commit 3719d49

Please sign in to comment.