Luke Renn on 9 Sep 2005 14:16:57 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] batch renaming


* Art Alexion <art.alexion@verizon.net> [2005-09-09 09:33:52 -0400]:

> in dos if I wanted to rename a batch of files i a directory, I could do
> 
>     move (or rn) ??.txt 61??.txt
> 
> and it would rename all of the two character-named text files to prepend
> 61.  Bash won't let me do this as
> 
>     mv ??.txt 61??.txt
> 
> returns an error indicating that. when moving multiple files, the target
> must be a directory.  Is there a command that will let me do this?
> 

This perl script is great.  It can do renames based on perl regular
expressions with substitution.

#!/usr/bin/perl
#
# Usage: rename perlexpr [files]

($regexp = shift @ARGV) || die "Usage:  rename perlexpr [filenames]\n";

if (!@ARGV) {
    @ARGV = <STDIN>;
    chomp(@ARGV);
}


foreach $_ (@ARGV) {
    $old_name = $_;
    eval $regexp;
    die $@ if $@;
    rename($old_name, $_) unless $old_name eq $_;
}


-- 
Luke Renn <luke.renn@gmail.com>
48D2 2171 A0A5 35B0 F917  D7D4 CAA5 2E6A 7617 0785

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug