diff --git a/README.md b/README.md new file mode 100644 index 0000000..735debc --- /dev/null +++ b/README.md @@ -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 diff --git a/rename b/rename new file mode 100755 index 0000000..aca5dba --- /dev/null +++ b/rename @@ -0,0 +1,82 @@ +#!/usr/bin/perl +# +# renames files according to a perl expression +# +# 2007-03-13 david@bushong.net +# +# 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 <; + 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}; +}