Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Rename files in bash via string handling' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Rename files in bash via string handling
Authored by: sanandak on Sep 08, '06 05:52:04AM

Here is a perl script that allows you to use ANY perl expression that converts one string to another - be careful!

% rename.pl 's/xyz/abc/' *

## strip .orig from filename
% rename.pl 's/.orig$//' *.orig

##make lc, except Makefile
% rename.pl 'tr/A-Z/a-z unless /^Make/' *

-----save this as rename.pl in your path, and chmod +x rename.pl to
-----make executable
<code>
#!/usr/bin/perl -w
# From T. Christiansen and N Torkington,
# The Perl Cookbook, O'Reilly, 1998
# Usage: rename.pl perlexpr [files]

($op = shift) || die "Usage: rename.pl perlexpr [filenames]\n";
if(!@ARGV) {
@ARGV=<STDIN>;
chop(@ARGV);
}
for (@ARGV) {
$was=$_;
eval $op;
die $@ if $@;
# print "$was ==> $_\n";
rename($was,$_) unless $was eq $_;
}
</code>
Sincerely,

Sridhar.



[ Reply to This | # ]